From 9a3173dacdac71855eb0454b9c5b4a388eedbcd4 Mon Sep 17 00:00:00 2001 From: Gwendolyn Kornak Date: Mon, 2 Jun 2025 18:59:21 -0700 Subject: [PATCH] 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. --- .../virtualisation/nixos-containers.nix | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/nixos/modules/virtualisation/nixos-containers.nix b/nixos/modules/virtualisation/nixos-containers.nix index e5a0d37d6a9c..e7f3c050bd49 100644 --- a/nixos/modules/virtualisation/nixos-containers.nix +++ b/nixos/modules/virtualisation/nixos-containers.nix @@ -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..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) ''