diff --git a/flake.nix b/flake.nix index b13cd90..41779a3 100644 --- a/flake.nix +++ b/flake.nix @@ -151,6 +151,8 @@ nh agenix.packages.${system}.default age + # yq-go — YAML linting used by `skyg encrypt --yaml` + yq home-manager.packages.${system}.home-manager rustc cargo diff --git a/hosts/hl-minipc2.nix b/hosts/hl-minipc2.nix index ecfe85a..3c6ad01 100644 --- a/hosts/hl-minipc2.nix +++ b/hosts/hl-minipc2.nix @@ -81,6 +81,31 @@ }; }; + # Drawdb + skyg.nixos.common.container-services.drawdb = { + enable = true; + autoUpdate.enable = true; + services.drawdb = { + image = "ghcr.io/drawdb-io/drawdb:latest"; + ports = [ + "3000:80" + ]; + }; + }; + + age.secrets.stack1.file = ../secrets/stack1.age; + skyg.nixos.common.container-services.stack1 = { + enable = true; + autoUpdate.enable = true; + composeFile = config.age.secrets.stack1.path; + }; + age.secrets.stack2.file = ../secrets/stack2.age; + skyg.nixos.common.container-services.stack2 = { + enable = true; + autoUpdate.enable = true; + composeFile = config.age.secrets.stack2.path; + }; + # Scrypted backup timer (image auto-update handled by container-services autoUpdate) skyg.server.timers = { scrypted-backups = { diff --git a/modules/nixos/common/containers/container-services/ABOUTME.md b/modules/nixos/common/containers/container-services/ABOUTME.md index 3f25f83..af7f39c 100644 --- a/modules/nixos/common/containers/container-services/ABOUTME.md +++ b/modules/nixos/common/containers/container-services/ABOUTME.md @@ -23,6 +23,11 @@ Custom compose-style container service groups for systemd. 6. `systemd.nix` builds oneshot units to manage the stack 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. +See `user-guide.md` → "Secret Compose Definitions". + ## Adding a new feature If adding a new per-service option: diff --git a/modules/nixos/common/containers/container-services/default.nix b/modules/nixos/common/containers/container-services/default.nix index d0957a9..91dd447 100644 --- a/modules/nixos/common/containers/container-services/default.nix +++ b/modules/nixos/common/containers/container-services/default.nix @@ -24,17 +24,25 @@ in config = { - assertions = lib.optionals (enabledGroups != { }) [ - { - assertion = config.virtualisation.docker.enable or false + assertions = + (lib.optionals (enabledGroups != { }) + [{ + assertion = config.virtualisation.docker.enable or false || config.virtualisation.podman.enable or false; - message = '' - skyg.nixos.common.container-services: at least one container runtime - must be enabled. Set skyg.nixos.common.containers.runtime (or enable - virtualisation.docker / virtualisation.podman directly). - ''; - } - ]; + message = '' + skyg.nixos.common.container-services: at least one container runtime + must be enabled. Set skyg.nixos.common.containers.runtime (or enable + virtualisation.docker / virtualisation.podman directly). + ''; + }]) + ++ lib.mapAttrsToList + (name: grpCfg: + { + assertion = grpCfg.composeFile == null || grpCfg.services == { }; + message = + "skyg.nixos.common.container-services.${name}: set either composeFile OR services, not both."; + }) + enabledGroups; # Create tmpfiles rules for state dirs and file dirs systemd.tmpfiles.rules = @@ -54,12 +62,17 @@ in (groupName: grpCfg: let fileVolumes = filesLib.mkFileVolumesForGroup groupName grpCfg; - composeFile = composeLib.mkComposeFile pkgs groupName grpCfg fileVolumes; - hasFiles = filesLib.getAllFiles grpCfg != { }; + # An external compose file (e.g. an agenix secret) is used verbatim; + # otherwise render one from the group's options. + effectiveComposeFile = + if grpCfg.composeFile != null + then grpCfg.composeFile + else composeLib.mkComposeFile pkgs groupName grpCfg fileVolumes; + hasFiles = grpCfg.composeFile == null && filesLib.getAllFiles grpCfg != { }; in lib.nameValuePair "container-services-${groupName}" - (systemdLib.mkSystemdService groupName grpCfg composeFile composeBin runtimeService hasFiles)) + (systemdLib.mkSystemdService groupName grpCfg effectiveComposeFile composeBin runtimeService hasFiles)) enabledGroups // lib.foldAttrs lib.recursiveUpdate { } ( lib.mapAttrsToList diff --git a/modules/nixos/common/containers/container-services/options.nix b/modules/nixos/common/containers/container-services/options.nix index 1584aed..3a8b5b7 100644 --- a/modules/nixos/common/containers/container-services/options.nix +++ b/modules/nixos/common/containers/container-services/options.nix @@ -30,6 +30,24 @@ description = "Seconds to wait for 'compose down' before forcibly killing containers."; }; + composeFile = lib.mkOption { + type = lib.types.nullOr lib.types.str; + default = null; + 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. + + 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. + ''; + }; + services = lib.mkOption { description = "Container services in this group (maps to compose services: block)."; default = { }; diff --git a/modules/nixos/common/containers/user-guide.md b/modules/nixos/common/containers/user-guide.md index a8f2104..e8c7c85 100644 --- a/modules/nixos/common/containers/user-guide.md +++ b/modules/nixos/common/containers/user-guide.md @@ -129,6 +129,38 @@ Files are: --- +## Secret Compose Definitions (External `composeFile`) + +Keep a whole stack's definition out of version control by storing it in an +agenix secret and pointing the group at it: + +```nix +# In your host file +age.secrets.my-stack = { + file = ../secrets/my-stack.age; +}; +skyg.nixos.common.container-services.my-stack = { + enable = true; + composeFile = config.age.secrets.my-stack.path; +}; +``` + +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. +- Requires exactly one of `composeFile` or `services` to be set (assertion). + +Notes: +- `env_file:` entries in the external file resolve **relative to** + `/var/lib/container-services//`. Use absolute paths (e.g. another + agenix secret path) or inline `environment:` instead. +- Create the secret with `skyg encrypt `. + +--- + ## Limitations - **One unit per group.** No per-container systemd units. Use `docker compose logs` for diff --git a/secrets.nix b/secrets.nix index a29c45d..daacf63 100644 --- a/secrets.nix +++ b/secrets.nix @@ -23,4 +23,8 @@ in "secrets/hermes-env.age".publicKeys = [ ari fwdesk ]; "secrets/iu-project.age".publicKeys = [ ari minipc1 ]; + + # stack compose definition — minipc2 + "secrets/stack1.age".publicKeys = [ ari minipc2 ]; + "secrets/stack2.age".publicKeys = [ ari minipc2 ]; } diff --git a/secrets/stack1.age b/secrets/stack1.age new file mode 100644 index 0000000..235a7d9 Binary files /dev/null and b/secrets/stack1.age differ diff --git a/secrets/stack2.age b/secrets/stack2.age new file mode 100644 index 0000000..371a8cb Binary files /dev/null and b/secrets/stack2.age differ