about summary refs log tree commit diff
path: root/pkgs/os-specific
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2024-01-18 17:21:00 +0100
committerRobert Hensing <robert@roberthensing.nl>2024-01-18 18:04:46 +0100
commit71154a6e3046f58517941f7144062adec27e2316 (patch)
tree4cc6a1f9c627fade500956026582129e6f0a078d /pkgs/os-specific
parent8be52599b7074027d7614eab8f4eb8d29b8f72ab (diff)
downloadnixlib-71154a6e3046f58517941f7144062adec27e2316.tar
nixlib-71154a6e3046f58517941f7144062adec27e2316.tar.gz
nixlib-71154a6e3046f58517941f7144062adec27e2316.tar.bz2
nixlib-71154a6e3046f58517941f7144062adec27e2316.tar.lz
nixlib-71154a6e3046f58517941f7144062adec27e2316.tar.xz
nixlib-71154a6e3046f58517941f7144062adec27e2316.tar.zst
nixlib-71154a6e3046f58517941f7144062adec27e2316.zip
nixos-rebuild: Test the repl subcommand
Diffstat (limited to 'pkgs/os-specific')
-rw-r--r--pkgs/os-specific/linux/nixos-rebuild/default.nix4
-rw-r--r--pkgs/os-specific/linux/nixos-rebuild/test/repl.nix140
2 files changed, 143 insertions, 1 deletions
diff --git a/pkgs/os-specific/linux/nixos-rebuild/default.nix b/pkgs/os-specific/linux/nixos-rebuild/default.nix
index 6c150b1b8cdb..1a8eb704eb43 100644
--- a/pkgs/os-specific/linux/nixos-rebuild/default.nix
+++ b/pkgs/os-specific/linux/nixos-rebuild/default.nix
@@ -1,4 +1,5 @@
-{ substituteAll
+{ callPackage
+, substituteAll
 , runtimeShell
 , coreutils
 , gnused
@@ -36,6 +37,7 @@ substituteAll {
   # run some a simple installer tests to make sure nixos-rebuild still works for them
   passthru.tests = {
     install-bootloader = nixosTests.nixos-rebuild-install-bootloader;
+    repl = callPackage ./test/repl.nix {};
     simple-installer = nixosTests.installer.simple;
     specialisations = nixosTests.nixos-rebuild-specialisations;
   };
diff --git a/pkgs/os-specific/linux/nixos-rebuild/test/repl.nix b/pkgs/os-specific/linux/nixos-rebuild/test/repl.nix
new file mode 100644
index 000000000000..0a057f933255
--- /dev/null
+++ b/pkgs/os-specific/linux/nixos-rebuild/test/repl.nix
@@ -0,0 +1,140 @@
+{ lib,
+  expect,
+  nix,
+  nixos-rebuild,
+  path,
+  runCommand,
+  stdenv,
+  writeText,
+}:
+let
+  # Arguably not true, but it holds up for now.
+  escapeExpect = lib.strings.escapeNixString;
+
+  expectSetup = ''
+    set timeout 180
+    proc expect_simple { pattern } {
+      puts "Expecting: $pattern"
+      expect {
+        timeout {
+          puts "\nTimeout waiting for: $pattern\n"
+          exit 1
+        }
+        $pattern
+      }
+    }
+  '';
+in
+runCommand "test-nixos-rebuild-repl" {
+  nativeBuildInputs = [
+    expect
+    nix
+    nixos-rebuild
+  ];
+  nixpkgs =
+    if builtins.pathExists (path + "/.git")
+    then lib.cleanSource path
+    else path;
+} ''
+  export HOME=$(mktemp -d)
+  export TEST_ROOT=$PWD/test-tmp
+
+  # Prepare for running Nix in sandbox
+  export NIX_BUILD_HOOK=
+  export NIX_CONF_DIR=$TEST_ROOT/etc
+  export NIX_LOCALSTATE_DIR=$TEST_ROOT/var
+  export NIX_LOG_DIR=$TEST_ROOT/var/log/nix
+  export NIX_STATE_DIR=$TEST_ROOT/var/nix
+  export NIX_STORE_DIR=$TEST_ROOT/store
+  export PAGER=cat
+  mkdir -p $TEST_ROOT $NIX_CONF_DIR
+
+  echo General setup
+  ##################
+
+  export NIX_PATH=nixpkgs=$nixpkgs:nixos-config=$HOME/configuration.nix
+  cat >> ~/configuration.nix <<EOF
+  {
+    boot.loader.grub.enable = false;
+    fileSystems."/".device = "x";
+    imports = [ ./hardware-configuration.nix ];
+  }
+  EOF
+
+  echo '{ }' > ~/hardware-configuration.nix
+
+
+  echo Test traditional NixOS configuration
+  #########################################
+
+  expect ${writeText "test-nixos-rebuild-repl-expect" ''
+    ${expectSetup}
+    spawn nixos-rebuild repl --fast
+
+    expect "nix-repl> "
+
+    send "config.networking.hostName\n"
+    expect "\"nixos\""
+  ''}
+
+
+  echo Test flake based NixOS configuration
+  #########################################
+
+  # Switch to flake flavored environment
+  unset NIX_PATH
+  cat > $NIX_CONF_DIR/nix.conf <<EOF
+  experimental-features = nix-command flakes
+  EOF
+
+  # Make the config pure
+  echo '{ nixpkgs.hostPlatform = "${stdenv.hostPlatform.system}"; }' > ~/hardware-configuration.nix
+
+  cat >~/flake.nix <<EOF
+  {
+    inputs.nixpkgs.url = "path:$nixpkgs";
+    outputs = { nixpkgs, ... }: {
+      nixosConfigurations.testconf = nixpkgs.lib.nixosSystem {
+        modules = [
+          ./configuration.nix
+          # Let's change it up a bit
+          { networking.hostName = "itsme"; }
+        ];
+      };
+    };
+  }
+  EOF
+
+  # cat -n ~/flake.nix
+
+  expect ${writeText "test-nixos-rebuild-repl-expect" ''
+    ${expectSetup}
+    spawn sh -c "nixos-rebuild repl --fast --flake path:\$HOME#testconf"
+
+    expect_simple "nix-repl>"
+
+    send "config.networking.hostName\n"
+    expect_simple "itsme"
+
+    expect_simple "nix-repl>"
+    send "lib.version\n"
+    expect_simple ${escapeExpect (
+      # The version string is a bit different in the flake lib, so we expect a prefix and ignore the rest
+      # Furthermore, including the revision (suffix) would cause unnecessary rebuilds.
+      # Note that a length of 4 only matches e.g. "24.
+      lib.strings.substring 0 4 (lib.strings.escapeNixString lib.version))}
+
+    # Make sure it's the right lib - should be the flake lib, not Nixpkgs lib.
+    expect_simple "nix-repl>"
+    send "lib?nixosSystem\n"
+    expect_simple "true"
+    expect_simple "nix-repl>"
+    send "lib?nixos\n"
+    expect_simple "true"
+  ''}
+  echo
+
+  #########
+  echo Done
+  touch $out
+''