new static ip and merge approach
This commit is contained in:
1 parent
2cc0e92481
commit
a7b9273296
9 files changed
+99
-36
No files matched your search
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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.
|
||||
'';
|
||||
};
|
||||
|
||||
|
||||
@@ -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}" = {
|
||||
|
||||
@@ -8,6 +8,7 @@ staged on disk, and managed by a systemd oneshot unit.
|
||||
| What | Path |
|
||||
| ---- | ---- |
|
||||
| Staged compose file | `/var/lib/container-services/<group>/compose.yml` |
|
||||
| Staged overrides file (composeFile groups with networks/volumes/extraConfig set) | `/var/lib/container-services/<group>/compose.overrides.yml` |
|
||||
| Nix store source | `/nix/store/…-compose.yml` (copied on each activation) |
|
||||
| Systemd unit | `container-services-<group>.service` |
|
||||
| Container logs | `docker compose -p <group> logs -f <service>` |
|
||||
@@ -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/<group>/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/<group>/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/<group>/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:
|
||||
|
||||
Reference in new issue
Block a user