From 1d76033154a0b7498c423244f90fb5ecfa62ca38 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 8 Oct 2024 10:17:05 +0200 Subject: [PATCH 1/4] Rename nixos/modules/profiles/{macos-builder.nix -> nix-builder-vm.nix} --- doc/packages/darwin-builder.section.md | 6 +++--- .../profiles/{macos-builder.nix => nix-builder-vm.nix} | 0 pkgs/top-level/darwin-packages.nix | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) rename nixos/modules/profiles/{macos-builder.nix => nix-builder-vm.nix} (100%) diff --git a/doc/packages/darwin-builder.section.md b/doc/packages/darwin-builder.section.md index ca8519c5bf5f..06358c790165 100644 --- a/doc/packages/darwin-builder.section.md +++ b/doc/packages/darwin-builder.section.md @@ -100,7 +100,7 @@ $ sudo launchctl kickstart -k system/org.nixos.nix-daemon darwin-builder = nixpkgs.lib.nixosSystem { system = linuxSystem; modules = [ - "${nixpkgs}/nixos/modules/profiles/macos-builder.nix" + "${nixpkgs}/nixos/modules/profiles/nix-builder-vm.nix" { virtualisation = { host.pkgs = pkgs; darwin-builder.workingDirectory = "/var/lib/darwin-builder"; @@ -158,7 +158,7 @@ in the example below and rebuild. darwin-builder = nixpkgs.lib.nixosSystem { system = linuxSystem; modules = [ - "${nixpkgs}/nixos/modules/profiles/macos-builder.nix" + "${nixpkgs}/nixos/modules/profiles/nix-builder-vm.nix" { virtualisation.host.pkgs = pkgs; virtualisation.darwin-builder.diskSize = 5120; @@ -185,6 +185,6 @@ nix-repl> darwin.linux-builder.nixosConfig.nix.package «derivation /nix/store/...-nix-2.17.0.drv» nix-repl> :p darwin.linux-builder.nixosOptions.virtualisation.memorySize.definitionsWithLocations -[ { file = "/home/user/src/nixpkgs/nixos/modules/profiles/macos-builder.nix"; value = 3072; } ] +[ { file = "/home/user/src/nixpkgs/nixos/modules/profiles/nix-builder-vm.nix"; value = 3072; } ] ``` diff --git a/nixos/modules/profiles/macos-builder.nix b/nixos/modules/profiles/nix-builder-vm.nix similarity index 100% rename from nixos/modules/profiles/macos-builder.nix rename to nixos/modules/profiles/nix-builder-vm.nix diff --git a/pkgs/top-level/darwin-packages.nix b/pkgs/top-level/darwin-packages.nix index f0bdad863e90..919e02702656 100644 --- a/pkgs/top-level/darwin-packages.nix +++ b/pkgs/top-level/darwin-packages.nix @@ -245,7 +245,7 @@ impure-cmds // appleSourcePackages // chooseLibs // { nixos = import ../../nixos { configuration = { imports = [ - ../../nixos/modules/profiles/macos-builder.nix + ../../nixos/modules/profiles/nix-builder-vm.nix ] ++ modules; # If you need to override this, consider starting with the right Nixpkgs From 4687820524450e9ef6ad896cdf5c1fe7c29539bd Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 8 Oct 2024 10:17:21 +0200 Subject: [PATCH 2/4] Document nixos/modules/profiles/nix-builder-vm.nix --- nixos/modules/profiles/nix-builder-vm.nix | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/nixos/modules/profiles/nix-builder-vm.nix b/nixos/modules/profiles/nix-builder-vm.nix index 08b2fce40011..00768b1297ba 100644 --- a/nixos/modules/profiles/nix-builder-vm.nix +++ b/nixos/modules/profiles/nix-builder-vm.nix @@ -1,3 +1,11 @@ +/* + This profile uses NixOS to create a remote builder VM to build Linux packages, + which can be used to build packages for Linux on other operating systems; + primarily macOS. + + It contains both the relevant guest settings as well as an installer script + that manages it as a QEMU virtual machine on the host. + */ { config, lib, options, ... }: let From 00355648f01d146250564d7756b06fd638bf9c30 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 8 Oct 2024 10:25:58 +0200 Subject: [PATCH 3/4] nixos/modules/profiles/macos-builder.nix: Restore as alias --- nixos/modules/profiles/macos-builder.nix | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 nixos/modules/profiles/macos-builder.nix diff --git a/nixos/modules/profiles/macos-builder.nix b/nixos/modules/profiles/macos-builder.nix new file mode 100644 index 000000000000..896bb6bc9fbd --- /dev/null +++ b/nixos/modules/profiles/macos-builder.nix @@ -0,0 +1,5 @@ +let lib = import ../../../lib; +in + lib.warnIf (lib.isInOldestRelease 2411) + "nixos/modules/profiles/macos-builder.nix has moved to nixos/modules/profiles/nix-builder-vm.nix; please update your NixOS imports." + ./nix-builder-vm.nix From a034fb50f79816c6738fb48b48503b09ea3b0132 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Tue, 8 Oct 2024 11:27:01 +0200 Subject: [PATCH 4/4] Format --- nixos/modules/profiles/nix-builder-vm.nix | 82 ++++++++++++++--------- 1 file changed, 51 insertions(+), 31 deletions(-) diff --git a/nixos/modules/profiles/nix-builder-vm.nix b/nixos/modules/profiles/nix-builder-vm.nix index 00768b1297ba..fcaca974f302 100644 --- a/nixos/modules/profiles/nix-builder-vm.nix +++ b/nixos/modules/profiles/nix-builder-vm.nix @@ -5,8 +5,13 @@ It contains both the relevant guest settings as well as an installer script that manages it as a QEMU virtual machine on the host. - */ -{ config, lib, options, ... }: +*/ +{ + config, + lib, + options, + ... +}: let keysDirectory = "/var/keys"; @@ -31,7 +36,10 @@ in ]; # swraid's default depends on stateVersion config.boot.swraid.enable = false; - options.boot.isContainer = lib.mkOption { default = false; internal = true; }; + options.boot.isContainer = lib.mkOption { + default = false; + internal = true; + }; } ]; @@ -67,13 +75,13 @@ in ''; }; workingDirectory = mkOption { - default = "."; - type = types.str; - example = "/var/lib/darwin-builder"; - description = '' - The working directory to use to run the script. When running - as part of a flake will need to be set to a non read-only filesystem. - ''; + default = "."; + type = types.str; + example = "/var/lib/darwin-builder"; + description = '' + The working directory to use to run the script. When running + as part of a flake will need to be set to a non read-only filesystem. + ''; }; hostPort = mkOption { default = 31022; @@ -160,26 +168,34 @@ in script = hostPkgs.writeShellScriptBin "create-builder" ( '' set -euo pipefail - '' + - # When running as non-interactively as part of a DarwinConfiguration the working directory - # must be set to a writeable directory. - (if cfg.workingDirectory != "." then '' - ${hostPkgs.coreutils}/bin/mkdir --parent "${cfg.workingDirectory}" - cd "${cfg.workingDirectory}" - '' else "") + '' - KEYS="''${KEYS:-./keys}" - ${hostPkgs.coreutils}/bin/mkdir --parent "''${KEYS}" - PRIVATE_KEY="''${KEYS}/${user}_${keyType}" - PUBLIC_KEY="''${PRIVATE_KEY}.pub" - if [ ! -e "''${PRIVATE_KEY}" ] || [ ! -e "''${PUBLIC_KEY}" ]; then - ${hostPkgs.coreutils}/bin/rm --force -- "''${PRIVATE_KEY}" "''${PUBLIC_KEY}" - ${hostPkgs.openssh}/bin/ssh-keygen -q -f "''${PRIVATE_KEY}" -t ${keyType} -N "" -C 'builder@localhost' - fi - if ! ${hostPkgs.diffutils}/bin/cmp "''${PUBLIC_KEY}" ${publicKey}; then - (set -x; sudo --reset-timestamp ${installCredentials} "''${KEYS}") - fi - KEYS="$(${hostPkgs.nix}/bin/nix-store --add "$KEYS")" ${lib.getExe config.system.build.vm} - ''); + '' + + + # When running as non-interactively as part of a DarwinConfiguration the working directory + # must be set to a writeable directory. + ( + if cfg.workingDirectory != "." then + '' + ${hostPkgs.coreutils}/bin/mkdir --parent "${cfg.workingDirectory}" + cd "${cfg.workingDirectory}" + '' + else + "" + ) + + '' + KEYS="''${KEYS:-./keys}" + ${hostPkgs.coreutils}/bin/mkdir --parent "''${KEYS}" + PRIVATE_KEY="''${KEYS}/${user}_${keyType}" + PUBLIC_KEY="''${PRIVATE_KEY}.pub" + if [ ! -e "''${PRIVATE_KEY}" ] || [ ! -e "''${PUBLIC_KEY}" ]; then + ${hostPkgs.coreutils}/bin/rm --force -- "''${PRIVATE_KEY}" "''${PUBLIC_KEY}" + ${hostPkgs.openssh}/bin/ssh-keygen -q -f "''${PRIVATE_KEY}" -t ${keyType} -N "" -C 'builder@localhost' + fi + if ! ${hostPkgs.diffutils}/bin/cmp "''${PUBLIC_KEY}" ${publicKey}; then + (set -x; sudo --reset-timestamp ${installCredentials} "''${KEYS}") + fi + KEYS="$(${hostPkgs.nix}/bin/nix-store --add "$KEYS")" ${lib.getExe config.system.build.vm} + '' + ); in script.overrideAttrs (old: { @@ -224,7 +240,11 @@ in memorySize = cfg.memorySize; forwardPorts = [ - { from = "host"; guest.port = 22; host.port = cfg.hostPort; } + { + from = "host"; + guest.port = 22; + host.port = cfg.hostPort; + } ]; # Disable graphics for the builder since users will likely want to run it