diff options
| -rw-r--r-- | CHANGELOG.md | 1 | ||||
| -rw-r--r-- | README.md | 6 | ||||
| -rw-r--r-- | pkgs/krops/default.nix | 11 | ||||
| -rwxr-xr-x | tests/nixos-rebuild.sh | 6 | ||||
| -rw-r--r-- | tests/scenarios.nix | 23 |
5 files changed, 41 insertions, 6 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 78721ea..c0f9380 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - `git` sources with a branch or tag as `ref` follow it: populate fetches the branch or tag on every run, and resets the work tree to what it fetched. +- `writeDeploy`'s new `allocateTTY` runs `nixos-rebuild` with a terminal. ## 1.31.0 @@ -135,6 +135,12 @@ pkgs.krops.writeDeploy "deploy" { For more details about the `target` attribute, please check the `mkTarget` function in [lib/default.nix](lib/default.nix). +### `allocateTTY` (optional, defaults to false) + +Run `nixos-rebuild` with a terminal, so it shows colours and progress. On a +remote target, this sets `-t` on the ssh command, which only allocates a +terminal if krops runs in one. On the local target, `unbuffer` provides one. + ### `backup` (optional, defaults to false) Backup all paths specified in source before syncing new sources. diff --git a/pkgs/krops/default.nix b/pkgs/krops/default.nix index ac2d9a2..d33bcb5 100644 --- a/pkgs/krops/default.nix +++ b/pkgs/krops/default.nix @@ -2,9 +2,10 @@ let lib = import ../../lib; in -{ nix, openssh, populate, writers }: rec { +{ expect, nix, openssh, populate, writers }: rec { rebuild = { + allocateTTY ? false, flake ? null, useNixOutputMonitor }: @@ -14,7 +15,7 @@ in flakeDir = lib.head (lib.splitString "#" flake); flakeAttr = lib.removePrefix flakeDir flake; in - runShell target {} + runShell target { inherit allocateTTY; } (withNixOutputMonitor target useNixOutputMonitor /* sh */ '' ${lib.optionalString (flake != null) /* sh */ '' ${lib.shell.requireFlakes "deploying a flake"} @@ -35,6 +36,9 @@ in fi ''} NIX_PATH=${lib.escapeShellArg target.path} \ + ${lib.optionalString (allocateTTY && lib.isLocalTarget target) + # Without ssh, which allocates the terminal on remote targets. + "${expect}/bin/unbuffer"} \ nixos-rebuild ${lib.escapeShellArgs args} ${ if flake != null then ''--flake "$flake"${lib.escapeShellArg flakeAttr}'' @@ -119,6 +123,7 @@ in ''; writeDeploy = name: { + allocateTTY ? false, backup ? false, buildTarget ? null, crossDeploy ? false, @@ -159,7 +164,7 @@ in ${lib.optionalString (buildTarget' != target') (populate { inherit backup force prune source; target = buildTarget'; })} ${populate { inherit backup force prune source; target = target'; }} - ${rebuild { flake = flake'; inherit useNixOutputMonitor; } ([ + ${rebuild { flake = flake'; inherit allocateTTY useNixOutputMonitor; } ([ operation ] ++ lib.optionals crossDeploy [ "--no-build-nix" diff --git a/tests/nixos-rebuild.sh b/tests/nixos-rebuild.sh index 9e2e47e..ec0c85c 100755 --- a/tests/nixos-rebuild.sh +++ b/tests/nixos-rebuild.sh @@ -1,8 +1,10 @@ #! /bin/sh -# Stands in for nixos-rebuild: records the user it runs as, NIX_PATH, and its -# arguments in /tmp/nixos-rebuild.log. +# Stands in for nixos-rebuild: records the user it runs as, NIX_PATH, its +# arguments, and whether its output is a terminal in /tmp/nixos-rebuild.log. +if test -t 1; then terminal=terminal; else terminal='no terminal'; fi { id -u printf '%s\n' "${NIX_PATH-}" printf '%s\n' "$*" + echo "$terminal" } > /tmp/nixos-rebuild.log diff --git a/tests/scenarios.nix b/tests/scenarios.nix index e618fcd..2fc1bd7 100644 --- a/tests/scenarios.nix +++ b/tests/scenarios.nix @@ -786,7 +786,28 @@ assert !(krops.isLocalTarget (targets.remote "/")); expect_eq arguments "$(sed -n 3p /tmp/nixos-rebuild.log)" "$expected" ''; }; - }); + }) + + # writeDeploy with allocateTTY: nixos-rebuild's output is a terminal on + # the local target. ssh allocates one on remote targets only if its own + # input is a terminal, which the tests' isn't. + ++ map (targetKind: scenario { + name = "deploy-${targetKind}-allocateTTY"; + inherit targetKind; + prepare = '' + rm -f /tmp/nixos-rebuild.log /etc/nixos/flake.nix + ''; + phases = [{ + sources.x.file = "/fixtures/alt"; + deploy.allocateTTY = true; + check = '' + expect_eq arguments "$(sed -n 3p /tmp/nixos-rebuild.log)" switch + expect_eq terminal "$(sed -n 4p /tmp/nixos-rebuild.log)" ${ + if targetKind == "local" then "terminal" else "'no terminal'" + } + ''; + }]; + }) targetKinds; # Features spanning several sources. features = concatMap (targetKind: let |
