From 228dc7eaf4252f1c95a3c5e639a170e3ba693d93 Mon Sep 17 00:00:00 2001 From: Andrew Marshall Date: Sat, 30 Mar 2024 18:15:08 -0400 Subject: [PATCH] nixos-rebuild: Fix `repl` with a relative flake path Most nixos-rebuild commands support a relative flake path, however `repl` uses `builtins.getFlake` and that requires an absolute path. So ensure we pass an absolute path to it, providing UX consistency. Only do so when the path exists in order to passthrough URLs as-is. --- .../linux/nixos-rebuild/nixos-rebuild.sh | 7 ++++++- .../os-specific/linux/nixos-rebuild/test/repl.nix | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh b/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh index 248dc7213888..30a1e4dd8b6f 100755 --- a/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh +++ b/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh @@ -559,11 +559,16 @@ if [ "$action" = repl ]; then blue="$(echo -e '\033[34;1m')" attention="$(echo -e '\033[35;1m')" reset="$(echo -e '\033[0m')" + if [[ -e $flake ]]; then + flakePath=$(realpath "$flake") + else + flakePath=$flake + fi # This nix repl invocation is impure, because usually the flakeref is. # For a solution that preserves the motd and custom scope, we need # something like https://github.com/NixOS/nix/issues/8679. exec nix repl --impure --expr " - let flake = builtins.getFlake ''$flake''; + let flake = builtins.getFlake ''$flakePath''; configuration = flake.$flakeAttr; motd = '' $d{$q\n$q} diff --git a/pkgs/os-specific/linux/nixos-rebuild/test/repl.nix b/pkgs/os-specific/linux/nixos-rebuild/test/repl.nix index 1161ff84664d..c17546851cbf 100644 --- a/pkgs/os-specific/linux/nixos-rebuild/test/repl.nix +++ b/pkgs/os-specific/linux/nixos-rebuild/test/repl.nix @@ -113,7 +113,7 @@ runCommand "test-nixos-rebuild-repl" { # cat -n ~/flake.nix - expect ${writeText "test-nixos-rebuild-repl-expect" '' + expect ${writeText "test-nixos-rebuild-repl-absolute-path-expect" '' ${expectSetup} spawn sh -c "nixos-rebuild repl --fast --flake path:\$HOME#testconf" @@ -138,6 +138,19 @@ runCommand "test-nixos-rebuild-repl" { send "lib?nixos\n" expect_simple "true" ''} + + pushd "$HOME" + expect ${writeText "test-nixos-rebuild-repl-relative-path-expect" '' + ${expectSetup} + spawn sh -c "nixos-rebuild repl --fast --flake .#testconf" + + expect_simple "nix-repl>" + + send "config.networking.hostName\n" + expect_simple "itsme" + ''} + popd + echo #########