nixos/nixos-containers: add flake option

While the nixos-container command allows for the creation of containers pointing to a flake, the declarative module doesn't have this option.
Adds the flake option for nixos-container declarative approach. Creates the /etc/nixos-container .conf file similar to how the command preforms it.
This commit is contained in:
Gwendolyn Kornak
2025-07-05 17:21:37 -07:00
committed by Jade Lovelace
parent 17b5150fcb
commit 9a3173dacd
@@ -844,6 +844,16 @@ in
'';
};
flake = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
example = "github:NixOS/nixpkgs/master";
description = ''
The Flake URI of the NixOS configuration to use for the container.
Replaces the option {option}`containers.<name>.path`.
'';
};
# Removed option. See `checkAssertion` below for the accompanying error message.
pkgs = mkOption { visible = false; };
} // networkOptions;
@@ -867,13 +877,18 @@ in
- containers.${name}.config.nixpkgs.pkgs
This only sets the `pkgs` argument used inside the container modules.
''
else if options.config.isDefined && (options.flake.value != null) then
throw ''
The options 'containers.${name}.path' and 'containers.${name}.flake' cannot both be set.
''
else
null;
in
{
path =
builtins.seq checkAssertion mkIf options.config.isDefined
config.config.system.build.toplevel;
path = builtins.seq checkAssertion mkMerge [
(mkIf options.config.isDefined config.config.system.build.toplevel)
(mkIf (config.flake != null) "/nix/var/nix/profiles/per-container/${name}")
];
};
}
)
@@ -1044,7 +1059,12 @@ in
name: cfg:
nameValuePair "${configurationDirectoryName}/${name}.conf" {
text = ''
SYSTEM_PATH=${cfg.path}
${optionalString (cfg.flake == null) ''
SYSTEM_PATH=${cfg.path}
''}
${optionalString (cfg.flake != null) ''
FLAKE=${cfg.flake}
''}
${optionalString cfg.privateNetwork ''
PRIVATE_NETWORK=1
${optionalString (cfg.hostBridge != null) ''