diff --git a/hosts/hl-minipc2.nix b/hosts/hl-minipc2.nix index 63ff156..25ef84c 100644 --- a/hosts/hl-minipc2.nix +++ b/hosts/hl-minipc2.nix @@ -17,6 +17,11 @@ let }; }; }; + staticIp = ip: { + lan = { + ipv4_address = ip; + }; + }; in { skyg.user.enable = true; @@ -76,11 +81,7 @@ in environment = { PORT = "80"; }; - networks = { - lan = { - ipv4_address = "10.0.101.2"; - }; - }; + networks = staticIp "10.0.101.2"; }; networks = macvlanNetwork; }; @@ -95,16 +96,18 @@ in "/var/run/docker.sock:/var/run/docker.sock" "portainer_data:/data" ]; - ports = [ - "9443:9443" - "8000:8000" - ]; + # ports = [ + # "9443:9443" + # "8000:8000" + # ]; + networks = staticIp "10.0.101.5"; }; volumes = { portainer_data = { name = "portainer_data"; }; }; + networks = macvlanNetwork; }; age.secrets.stack1.file = ../secrets/stack1.age; @@ -112,12 +115,14 @@ in enable = true; autoUpdate.enable = true; composeFile = config.age.secrets.stack1.path; + networks = macvlanNetwork; }; age.secrets.stack2.file = ../secrets/stack2.age; skyg.nixos.common.container-services.stack2 = { enable = true; autoUpdate.enable = true; composeFile = config.age.secrets.stack2.path; + networks = macvlanNetwork; }; # Scrypted backup timer (image auto-update handled by container-services autoUpdate) diff --git a/modules/nixos/common/containers/container-services/ABOUTME.md b/modules/nixos/common/containers/container-services/ABOUTME.md index af7f39c..feca318 100644 --- a/modules/nixos/common/containers/container-services/ABOUTME.md +++ b/modules/nixos/common/containers/container-services/ABOUTME.md @@ -9,7 +9,7 @@ Custom compose-style container service groups for systemd. | `default.nix` | Main module entry point; imports options and wires everything | | `options.nix` | All `skyg.nixos.common.container-services.*` option declarations | | `lib.nix` | Shared helpers (runtime dispatch, path sanitization, env file collection) | -| `compose.nix` | YAML composition (mkComposeService, mkComposeAttrs, mkComposeFile) | +| `compose.nix` | YAML composition (mkComposeService, mkComposeAttrs, mkComposeFile, mkOverridesFile) | | `files.nix` | File mounting logic (getAllFiles, mkFileVolumesForService, mkFilesService) | | `systemd.nix` | Systemd unit builders (mkSystemdService, mkPathUnit, mkEnvReloadService) | @@ -24,8 +24,11 @@ Custom compose-style container service groups for systemd. 7. `default.nix` wires tmpfiles, services, and paths together A group may instead set `composeFile` (e.g. to an agenix secret path) to use an -external, ready-made compose file verbatim instead of rendering from options — -useful for keeping sensitive definitions (NFS, env) out of version control. +external, ready-made compose file for `services` instead of rendering from +options — useful for keeping sensitive definitions (NFS, env) out of version +control. `networks`/`volumes`/`extraConfig` set alongside `composeFile` still +apply: they're rendered as a base overrides file merged in via an earlier +compose `-f` flag, with `composeFile` itself taking precedence on overlaps. See `user-guide.md` → "Secret Compose Definitions". ## Adding a new feature diff --git a/modules/nixos/common/containers/container-services/compose.nix b/modules/nixos/common/containers/container-services/compose.nix index f16c0f1..e539238 100644 --- a/modules/nixos/common/containers/container-services/compose.nix +++ b/modules/nixos/common/containers/container-services/compose.nix @@ -77,4 +77,22 @@ rec { mkComposeFile = pkgs: groupName: grpCfg: fileVolumesByService: (pkgs.formats.yaml { }).generate "compose.yml" (mkComposeAttrs groupName grpCfg fileVolumesByService); + + # Build the overrides attrset (networks/volumes/extraConfig only) used + # alongside an external composeFile. Passed as an earlier -f file so the + # composeFile's own definitions win on any overlapping keys. + mkOverridesAttrs = grpCfg: + lib.optionalAttrs (grpCfg.networks != { }) + { + networks = grpCfg.networks; + } + // lib.optionalAttrs (grpCfg.volumes != { }) { + volumes = grpCfg.volumes; + } + // grpCfg.extraConfig; + + # Render the overrides file to the Nix store. + mkOverridesFile = pkgs: groupName: grpCfg: + (pkgs.formats.yaml { }).generate "compose-overrides-${groupName}.yml" + (mkOverridesAttrs grpCfg); } diff --git a/modules/nixos/common/containers/container-services/default.nix b/modules/nixos/common/containers/container-services/default.nix index 91dd447..b07bb36 100644 --- a/modules/nixos/common/containers/container-services/default.nix +++ b/modules/nixos/common/containers/container-services/default.nix @@ -18,6 +18,15 @@ let autoUpdateGroups = lib.filterAttrs (_: g: g.autoUpdate.enable) enabledGroups; optionsModule = import ./options.nix { inherit lib; }; + # When composeFile is set, networks/volumes/extraConfig declared alongside it + # are rendered as a base overrides file merged in via an earlier -f flag, so + # the composeFile's own definitions win on any overlapping keys. + mkOverridesFileFor = groupName: grpCfg: + if grpCfg.composeFile != null + && (grpCfg.networks != { } || grpCfg.volumes != { } || grpCfg.extraConfig != { }) + then composeLib.mkOverridesFile pkgs groupName grpCfg + else null; + in { options = optionsModule.options; @@ -62,17 +71,18 @@ in (groupName: grpCfg: let fileVolumes = filesLib.mkFileVolumesForGroup groupName grpCfg; - # An external compose file (e.g. an agenix secret) is used verbatim; - # otherwise render one from the group's options. + # An external compose file (e.g. an agenix secret) is used for + # services; otherwise render one from the group's options. effectiveComposeFile = if grpCfg.composeFile != null then grpCfg.composeFile else composeLib.mkComposeFile pkgs groupName grpCfg fileVolumes; + overridesFile = mkOverridesFileFor groupName grpCfg; hasFiles = grpCfg.composeFile == null && filesLib.getAllFiles grpCfg != { }; in lib.nameValuePair "container-services-${groupName}" - (systemdLib.mkSystemdService groupName grpCfg effectiveComposeFile composeBin runtimeService hasFiles)) + (systemdLib.mkSystemdService groupName grpCfg effectiveComposeFile overridesFile composeBin runtimeService hasFiles)) enabledGroups // lib.foldAttrs lib.recursiveUpdate { } ( lib.mapAttrsToList @@ -92,7 +102,7 @@ in ) // lib.mapAttrs' (groupName: grpCfg: - let units = systemdLib.mkUpdateUnits groupName grpCfg composeBin runtimeBin; + let units = systemdLib.mkUpdateUnits groupName grpCfg (mkOverridesFileFor groupName grpCfg) composeBin runtimeBin; in lib.nameValuePair "container-services-${groupName}-update" units.services."container-services-${groupName}-update") autoUpdateGroups; @@ -109,7 +119,7 @@ in systemd.timers = lib.mapAttrs' (groupName: grpCfg: - let units = systemdLib.mkUpdateUnits groupName grpCfg composeBin runtimeBin; + let units = systemdLib.mkUpdateUnits groupName grpCfg (mkOverridesFileFor groupName grpCfg) composeBin runtimeBin; in lib.nameValuePair "container-services-${groupName}-update" units.timers."container-services-${groupName}-update") autoUpdateGroups; }; diff --git a/modules/nixos/common/containers/container-services/options.nix b/modules/nixos/common/containers/container-services/options.nix index e01b96d..c34da96 100644 --- a/modules/nixos/common/containers/container-services/options.nix +++ b/modules/nixos/common/containers/container-services/options.nix @@ -36,15 +36,19 @@ example = "config.age.secrets.my-compose.path"; description = '' Optional path to a ready-made compose file (e.g. an agenix-decrypted - secret such as config.age.secrets.my-compose.path) to use verbatim as - the group's compose.yml, instead of rendering one from - services/volumes/networks. + secret such as config.age.secrets.my-compose.path) to use as the + group's compose.yml, instead of rendering one from services. - When set, this group's services/volumes/networks/files blocks are - ignored; the external file must be a complete compose document. Use - this to keep a full definition (NFS devices, images, env, etc.) out - of version control while still deploying it through - container-services. + When set, this group's services/files blocks are ignored; the + external file must define services itself. However, networks/ + volumes/extraConfig set alongside composeFile ARE used: they're + rendered into a base overrides file passed to compose via an + earlier -f flag, so the composeFile is merged in as an override on + top (compose's native multi-file merge — composeFile's own + definitions win on any overlapping keys). Use this to keep a full + service definition (NFS devices, images, env, etc.) out of version + control while still deploying it through container-services, with + shared/host-specific networks or volumes still declared in Nix. ''; }; diff --git a/modules/nixos/common/containers/container-services/systemd.nix b/modules/nixos/common/containers/container-services/systemd.nix index 71a22e3..94a1a0d 100644 --- a/modules/nixos/common/containers/container-services/systemd.nix +++ b/modules/nixos/common/containers/container-services/systemd.nix @@ -2,10 +2,14 @@ { # Create a systemd oneshot service that manages the compose stack. - mkSystemdService = groupName: grpCfg: composeFile: composeBin: runtimeService: hasFiles: + mkSystemdService = groupName: grpCfg: composeFile: overridesFile: composeBin: runtimeService: hasFiles: let stateDir = grpCfg.stateDir; fileServiceDep = if hasFiles then "container-services-${groupName}-files.service" else null; + composeFileArgs = + if overridesFile != null + then "-f ${stateDir}/compose.overrides.yml -f ${stateDir}/compose.yml" + else "-f ${stateDir}/compose.yml"; in { description = "Container service group '${groupName}'"; @@ -29,19 +33,26 @@ Type = "oneshot"; RemainAfterExit = true; TimeoutStopSec = grpCfg.timeoutStopSec; - ExecStartPre = "${pkgs.coreutils}/bin/cp ${composeFile} ${stateDir}/compose.yml"; - ExecStart = "${composeBin} -p ${groupName} -f ${stateDir}/compose.yml up -d --remove-orphans"; - ExecStop = "${composeBin} -p ${groupName} -f ${stateDir}/compose.yml down"; + ExecStartPre = + [ "${pkgs.coreutils}/bin/cp ${composeFile} ${stateDir}/compose.yml" ] + ++ lib.optional (overridesFile != null) + "${pkgs.coreutils}/bin/cp ${overridesFile} ${stateDir}/compose.overrides.yml"; + ExecStart = "${composeBin} -p ${groupName} ${composeFileArgs} up -d --remove-orphans"; + ExecStop = "${composeBin} -p ${groupName} ${composeFileArgs} down"; }; }; # Create the update service + timer for a group's autoUpdate config. - mkUpdateUnits = groupName: grpCfg: composeBin: runtimeBin: + mkUpdateUnits = groupName: grpCfg: overridesFile: composeBin: runtimeBin: let auCfg = grpCfg.autoUpdate; stateDir = grpCfg.stateDir; unitName = "container-services-${groupName}-update"; - composeCmd = "${composeBin} -p ${groupName} -f ${stateDir}/compose.yml"; + composeFileArgs = + if overridesFile != null + then "-f ${stateDir}/compose.overrides.yml -f ${stateDir}/compose.yml" + else "-f ${stateDir}/compose.yml"; + composeCmd = "${composeBin} -p ${groupName} ${composeFileArgs}"; in { services."${unitName}" = { diff --git a/modules/nixos/common/containers/user-guide.md b/modules/nixos/common/containers/user-guide.md index e8c7c85..6cb6afb 100644 --- a/modules/nixos/common/containers/user-guide.md +++ b/modules/nixos/common/containers/user-guide.md @@ -8,6 +8,7 @@ staged on disk, and managed by a systemd oneshot unit. | What | Path | | ---- | ---- | | Staged compose file | `/var/lib/container-services//compose.yml` | +| Staged overrides file (composeFile groups with networks/volumes/extraConfig set) | `/var/lib/container-services//compose.overrides.yml` | | Nix store source | `/nix/store/…-compose.yml` (copied on each activation) | | Systemd unit | `container-services-.service` | | Container logs | `docker compose -p logs -f ` | @@ -142,15 +143,26 @@ age.secrets.my-stack = { skyg.nixos.common.container-services.my-stack = { enable = true; composeFile = config.age.secrets.my-stack.path; + # Optional: merged in as a base layer, overridden by composeFile itself + # on any overlapping keys. + networks = { + lan = { + driver = "macvlan"; + driver_opts.parent = "enp2s0"; + }; + }; }; ``` When `composeFile` is set, the module: -- Uses that file **verbatim** as the group's `compose.yml` (copied to - `/var/lib/container-services//compose.yml` at startup, just like a - rendered one). -- Ignores this group's `services`/`volumes`/`networks`/`files` blocks — the - external file must be a complete compose document. +- Copies that file to `/var/lib/container-services//compose.yml` at + startup, just like a rendered one. It must define `services` itself — + ignores this group's `services`/`files` blocks. +- If this group also sets `networks`/`volumes`/`extraConfig`, those are + rendered to `/var/lib/container-services//compose.overrides.yml` and + passed to compose **before** the composeFile (`-f compose.overrides.yml -f + compose.yml`), so compose's native multi-file merge applies — + composeFile's own definitions win on any overlapping keys. - Requires exactly one of `composeFile` or `services` to be set (assertion). Notes: diff --git a/secrets/stack1.age b/secrets/stack1.age index 235a7d9..0ebe709 100644 Binary files a/secrets/stack1.age and b/secrets/stack1.age differ diff --git a/secrets/stack2.age b/secrets/stack2.age index 371a8cb..916b485 100644 Binary files a/secrets/stack2.age and b/secrets/stack2.age differ