Commit Graph

61 Commits

Author SHA1 Message Date
Samuel Dionne-Riel
c95728ce72
Merge pull request #262684 from samueldr/feature/nixos-rebuild-completions
nixos-rebuild: Locally own the nixos-rebuild (bash) completions
2023-11-05 22:41:03 -05:00
Thiago Kenji Okada
ccebc89954 nixos/tests: add nixos-rebuild-install-bootloader 2023-10-23 12:39:40 +01:00
Thiago Kenji Okada
b269e6853e nixos-rebuild: fix --install-bootloader flag with systemd-run 2023-10-22 12:19:59 +01:00
Samuel Dionne-Riel
a5e30e71b6 nixos-rebuild: Locally own the nixos-rebuild completion 2023-10-22 03:18:50 -04:00
Samuel Dionne-Riel
61d5040ef1 nixos-rebuild: Drop incorrectly implemented short args for build/target host
*Nobody* uses them, or else it'd have been fixed already.
2023-10-22 02:42:48 -04:00
Thiago Kenji Okada
ecd89093e1 nixos-rebuild: run activation inside systemd-run
Right now, running `nixos-rebuild switch` in a remote system via SSH may
put the system in an unusable state by restarting services (e.g.:
network ones like systemd-networkd.service) during the update. This will
cause the SSH to lose access to the TTY, stopping the process entirely.

This commit wraps up the call to `switch-to-configuration` inside a
systemd-run call, making the switch resiliant against TTY loss, since it
will allocate its own TTY. For the user this should be entirely
transparent though, with the only visible change is that the
`switch-to-configuration` messages will be logged through journalctl
(e.g.: `journalctl -u nixos-rebuild-switch-to-configuration`).
2023-10-04 20:45:23 +01:00
Michael Lohmann
cc625486c4 nixos-rebuild: Add list-generations
Add new command `nixos-rebuild list-generations`. It will show an output
like

```
$ nixos-rebuild list-generations
Generation      Build-date               NixOS version           Kernel  Configuration Revision                    Specialisations
52   (current)  Fri 2023-08-18 08:17:27  23.11.20230817.0f46300  6.4.10  448160aeccf6a7184bd8a84290d527819f1c552c  *
51              Mon 2023-08-07 17:56:41  23.11.20230807.31b1eed  6.4.8   99ef480007ca51e3d440aa4fa6558178d63f9c42  *
```

This also mentions the change in the upcoming release notes
2023-09-10 15:13:04 +02:00
Doron Behar
68e6cc5831 nixos-rebuild: Include manual page in the package 2023-08-12 12:52:46 +03:00
Ilan Joselevich
fdf0c98ff4
nixos-rebuild: make --use-substitutes work with flakes 2023-07-08 00:11:51 +03:00
apfelkuchen06
619b142a7f nixos-rebuild: allow passing --log-format
This is benefitial when using nix-output-monitor.
2023-05-13 15:33:05 +02:00
Nick Cao
3cd694d1bd
Merge pull request #210382 from B4dM4n/nixos-rebuild-local
nixos-rebuild: Allow local builds when --target-host is used again
2023-01-23 16:37:41 +08:00
Patryk Wychowaniec
2c55eba8f4
nixos: add --specialisation to nixos-rebuild
This commit fixes a papercut in nixos-rebuild where people wanting to
switch to a specialisation (or test one) were forced to manually figure
out the specialisation's path and run its activation script - since now,
there's a dedicated option to do just that.

This is a backwards-compatible change which doesn't affect the existing
behavior, which - to be fair - might still be considered sus by some
people, the painful scenario here being:

- you boot into specialisation `foo`,
- you run `nixos-rebuild switch`,
- whoops, you're no longer at specialisation `foo`, but you're rather
  brought back to the base system.

(it's especially painful for cases where specialisation is used to load
extra drivers, e.g. Nvidia, since then launching `nixos-rebuild switch`,
while forgetting that you're inside a specialisation, can cause some
parts of your system to get accidentally unloaded.)

I've tried to mitigate that by improving specialisations so that they
create a dedicated file somewhere in `/run/current-system` containing
the specialisation's name (which `nixos-rebuild` could then use as the
default value for `--specialisation`), but I haven't been able to come
up with anything working (plus it would be a breaking change then).

Closes https://github.com/NixOS/nixpkgs/issues/174065
2023-01-15 18:16:49 +01:00
Fabian Möller
cc4de1aa3a
nixos-rebuild: Allow local builds when --target-host is used again
This is a followup of #148921, to allow local builds when
`--target-host` is used again. It also documents the change in
behavior, regarding the specialty of the `localhost` value.

By removing the special handling of an empty `buildHost` and non empty
`targetHost`, this change also slightly alters the behavior of
`nixos-rebuild`.

Originally by specifying `--target-host target --build-host ""`, the
now removed special case would transform those arguments to
`--target-host target --build-host target`.
Now the empty `--build-host` would result in a local build.
2023-01-13 10:16:46 +01:00
Victor Engmark
cbd1dc1e72 nixos-rebuild: Treat any build/target host as non-local
Closes #148918.
2023-01-01 00:28:02 -05:00
K900
28dae620b2 nixos-rebuild: always set flakeFlags
Otherwise a rebuild can fail when a flake is autodetected
in /etc/nixos/flake.nix and the system doesn't have flakes
globally enabled.
2022-07-29 10:05:18 +03:00
Marco Rebhan
0a71372a90
nixos-rebuild: Don't create out-link in PWD with test/dry-activate 2022-07-18 16:03:04 +02:00
Klemens Nanni
7a0549387b nixos-rebuild: Accept only one argument
Multiple arguments make no sense but they are accepted, the last one
winning.

Found editing a previous run from shell history and adding a command
rather than replacing it;  observe in verbose mode:

```
$ nixos-rebuild dry-run -v
building the system configuration...
Building in legacy (non-flake) mode.
No --build-host given, running nix-build locally
$ nix-build <nixpkgs/nixos> -A system -k -I nixpkgs=/home/kn/src/nixpkgs -v --dry-run
...
```

```
$ nixos-rebuild dry-run build -v
building Nix...
$ nix-instantiate <nixpkgs/nixos> --add-root /tmp/nixos-rebuild.jgEYqZ/nix.drv --indirect -A config.nix.package.out -I nixpkgs=/home/kn/src/nixpkgs -v
...
```

nixos-rebuild(8) already bails out on zero arguments, so do the same
when passing a second one.
2022-06-23 21:39:13 +04:00
Silvan Mosberger
204366af6d
Merge pull request #153981 from Enzime/nixos-rebuild-flake-reexec
nixos-rebuild: Reexec when using flakes
2022-03-25 18:16:22 +01:00
Profpatsch
a701c09286 nixos-rebuild: use log instead of echo, always print to stderr
Introduces a `log()` function, similar to `logVerbose()`, to replace
usage of `echo` for logging.
2022-03-23 11:49:59 +01:00
Sandro
46ddd465b9
Merge pull request #165302 from Profpatsch/nixos-rebuild-meta 2022-03-23 03:36:18 +01:00
Michal Sojka
611e9302f9 nixos-rebuild: add --no-flake switch
When a NixOS system uses flakes, i.e., /etc/nixos/flake.nix exists, it
is impossible to use nixos-rebuild to build a pre-flake
configuration.nix. Of course, one can directly use nix command to
build the configuration, but not everybody remembers the correct nix
options to do that.

With the new option, it is possible to build a pre-flake configuration
with command like this:

    nixos-rebuild build-vm -I nixos-config=./vm.nix --no-flake

The option might be useful for people following older pre-flake
tutorials on a flake-based system.
2022-03-22 19:03:10 +01:00
Profpatsch
c7172046c7 nixos-rebuild: add meta & add Profpatsch as maintainer 2022-03-22 18:35:33 +01:00
Maëlys Bras de fer
488869f602 nixos-rebuild: support --quiet, --print-build-logs
--quiet decreases the logging verbosity level (inverse of -v)
--print-build-logs prints build logs on stderr (same as -L)
Also reordered and grouped some options for consistency
2022-03-22 18:24:27 +01:00
Michael Hoang
c1bc8de9e3 nixos-rebuild: Reexec when using flakes 2022-03-22 07:28:15 +11:00
Profpatsch
5679b2b698 nixos-rebuild: add installer test to passthru.tests
When something changes in nixos-rebuild, we must make sure the
installers still work, otherwise it might break existing setups.

The installer tests check whether nixos-rebuild works, so they are a
good lithmus test.
2022-03-19 15:13:50 +01:00
Profpatsch
cb7bc7c60f nixos-rebuild: Print commands as they are run on verbose flag
nixos-rebuild would forward all verbose flags to nix, but not actually
increase its own verbosity.
Which would make it hard to see what’s going on without resorting to
tools like strace or adding `set -x` to the script.

We add a simple exec wrapper around the most “interesting” commands,
that is nix tool invocations. If any verbosity flag is given, it will
set the verbosity flag of the nixos-rebuild script all commands
wrapped into `runCmd` will start logging their invocation.

This is done via a `logVerbose` function, which can also be used to
print logging messages when in verbose mode, to aid debugging why
nixos-rebuild does what it does. A few messages are added, but
potentially we could log a lot more details.

Now, testing all of the changed code paths turns out to be rather
hard, so I am unsure how to verify that for all changed commands
behaviour stays indeed the same.
2022-03-19 15:13:50 +01:00
Charlotte Van Petegem
a2c0cb0db7 nixos-rebuild: Don't pass --impure to realization when building flake remotely and impurely
It doesn't work and it's not required for building.
2022-02-11 14:20:42 +01:00
K900
dd37d07ccf nixos-rebuild: also forward the long version of -L
Mostly for more readability in shell scripts.
2022-02-08 11:56:35 -08:00
Thiago Kenji Okada
c75bc3abc7 nixos-rebuild: remove jq
Was only used in the code removed in commit
c274d045ac.
2022-01-04 20:02:47 -03:00
Thiago Kenji Okada
c274d045ac nixos-rebuild: do not resolve flake path
The removed lines converted the flake path passed by the command line
from `/some/path` to `git+file:///some/path`.

This technically shouldn't cause any issues, however running
`nixos-rebuild switch` inside a directory `/nix/store` will cause the
switch to fail and leave a partially construct generation (see issue #144811
for details).

By itself this shouldn't be too much of an issue, however thanks to
another issue in `systemd-boot-builder.py` this can leave the system
in a broken state for those using `boot.loader.systemd-boot` (AFAIK the
default), where future `nixos-rebuild switch` will fail
(see issue #93694 for details).

The issue can be fixed by running
`nix-env -p /nix/var/nix/profiles/system --delete-generations old`,
however this makes newbies very confused and it is showing in our
support threads in Matrix and Discourse (see
https://discourse.nixos.org/t/need-help-on-failure-of-building-my-configuration/16842).

Keep in mind this is a workaround. The actual issue seems to be in nix
itself (see: https://github.com/NixOS/nix/issues/5510).

See also #150065 for an alternative fix that caused other issues.

Kudos for @figsoda for figuring out this fix.
2022-01-04 18:31:08 -03:00
Thiago Kenji Okada
e6ca3fc976 Revert "nixos-rebuild: switch to tmpDir during rebuilds"
This seems to break
`boot.kernelPackages = config.boot.zfs.package.latestCompatibleLinuxPackages`
causing it to use `linuxPackages`.
2021-12-16 14:47:07 -03:00
Thiago Kenji Okada
f88bd76fcd nixos-rebuild: switch to tmpDir during rebuilds
This is a workaround for issue #144811 until this issue is either fixed
on nix itself.
2021-12-10 10:50:46 -03:00
Victor Engmark
82b9b42682
Fix some nixos-rebuild lints (#147449) 2021-11-29 05:48:23 +02:00
Anund
bc5a610cb7 nixos-rebuild: fix --install-bootloader
nixos-rebuild relies on setting an environment variable to change
behaviour during nixos-rebuild boot/switch between calling update or
install via systemd bootctl. Adding sudo -- as a prefix to various exec
calls does not preserve the environment breaking this mechanism.

This is a some what ugly fix that works.

Closes https://github.com/NixOS/nixpkgs/pull/144429
2021-11-17 09:28:05 +02:00
Timothy DeHerrera
6dbeb3190d
Merge pull request #126544 from jonringer/nixos-rebuild-use-substitutes
nixos-rebuild: add --use-substitutes option
2021-09-09 13:53:37 -06:00
Sandro
78566493e8
Merge pull request #126614 from jonringer/nixos-rebuild-prefer-localhost
nixos-rebuild: default buildHost to localhost
2021-08-20 22:33:22 +02:00
brprice
9b0a99f64c
nixos-rebuild: print run-*-vm location with bootloader (#130385)
Previously when doing a nixos-rebuild build-vm we see a message saying how to
run the VM, but with nixos-rebuild build-vm-with-bootloader we did not. We
now show it in both cases.
2021-07-31 16:54:37 +02:00
Eelco Dolstra
0ad27c8653 nixos-rebuild: Set inherit_errexit
Without this, failure of nixBuild() and nixFlakeBuild() was ignored
(since bash doesn't inherit 'set -e' in subshells by default), so the
script would proceed with a bogus ./result link, e.g.

  ++ readlink -f /tmp/nixos-rebuild.NfHKxx/result
  + pathToConfig='/nix/store/m7dvk6an18cpr95qn5wnig2600qhv6w7-nix-2.4pre20210727_706777a/bin/nix
  /tmp/nixos-rebuild.NfHKxx/result'
  + '[' test = switch -o test = boot ']'
  + copyToTarget '/nix/store/m7dvk6an18cpr95qn5wnig2600qhv6w7-nix-2.4pre20210727_706777a/bin/nix
  /tmp/nixos-rebuild.NfHKxx/result'
  + '[' '' = '' ']'
  + '[' test = switch -o test = boot -o test = test -o test = dry-activate ']'
  + targetHostCmd /nix/store/m7dvk6an18cpr95qn5wnig2600qhv6w7-nix-2.4pre20210727_706777a/bin/nix /tmp/nixos-rebuild.NfHKxx/result/bin/switch-to-configuration test
  + '[' -z '' ']'
  + sudo -- /nix/store/m7dvk6an18cpr95qn5wnig2600qhv6w7-nix-2.4pre20210727_706777a/bin/nix /tmp/nixos-rebuild.NfHKxx/result/bin/switch-to-configuration test
  error: '/tmp/nixos-rebuild.NfHKxx/result/bin/switch-to-configuration' is not a recognised command
  Try '/nix/store/m7dvk6an18cpr95qn5wnig2600qhv6w7-nix-2.4pre20210727_706777a/bin/nix --help' for more information.
  + echo 'warning: error(s) occurred while switching to the new configuration'
  warning: error(s) occurred while switching to the new configuration
2021-07-28 17:13:01 +02:00
rnhmjoj
35a8d78e44
nixos-rebuild: fix --use-remote-sudo
Currently fails with:

  $ nixos-rebuild test --target-host myhost --use-remote-sudo
  building Nix...
  sudo: /run/current-system/sw/bin/sudo must be owned by uid 0 and have the setuid bit set

It seems setting an explicit PATH in the ssh command overrides the
remote setuid wrappers.
2021-06-18 02:55:21 +02:00
Samuel Gräfenstein
ff3b3e696a nixos-rebuild: fix creating ./result symlink for flakes 2021-06-15 08:15:31 -07:00
Jonathan Ringer
bf185eaa69
nixos-rebuild: add --use-substitutes option 2021-06-11 12:46:16 -07:00
Jonathan Ringer
971eb04341 nixos-rebuild: fix extraBuildFlags usage with flakes 2021-06-11 10:35:14 -07:00
Jonathan Ringer
d6a829e7b7 nixos-rebuild: remove unused variable: remotePATH 2021-06-11 10:35:14 -07:00
Jonathan Ringer
07da819bcf nixos-rebuild: prevent wordsplitting 2021-06-11 10:35:14 -07:00
Jonathan Ringer
0e8e7968d1 nixos-rebuild: prevent masking return value with declaration 2021-06-11 10:35:14 -07:00
Jonathan Ringer
ea5956a2ed nixos-rebuild: remove repair, never used 2021-06-11 10:35:14 -07:00
Jonathan Ringer
3d34fe9f2b
nixos-rebuild: default buildHost to localhost
Prior to #119540, the toplevel output was built
locally then pushed to the remote host when using
the command:

nixos-rebuild --flake <flake> --target-host remote.host

However, buildHost would default to targetHost when
only --target-host was supplied. This caused the .drv
closure to always be transferred unless --build-host=localhost
is supplied.

This change restores the previous build local default behavior.
TargetHost can still build the outputs, if explicitly mentioned:

nixos-rebuild --flake <flake> \
  --target-host remote.host \
  --build-host  remote.host
2021-06-11 10:01:21 -07:00
Chuck
1c80856545 nixos-rebuild: Pass flakes flags when doing local flakes build 2021-06-10 10:27:32 -07:00
Eelco Dolstra
4cffb1212a nixos-rebuild --fast: Don't imply --show-trace
There is no logical reason for --fast to imply --show-trace, and this
seems to be a historical accident. Using --show-trace by default is
bad UX since it can give very long error messages (e.g. 550 lines for
a non-existent attribute in environment.systemPackages).
2021-06-03 19:01:09 +02:00
Robert Hensing
3c6fcc9073
Merge pull request #122987 from alyssais/nixos-rebuild-aarch64
nixos-rebuild: support prebuilt Nix on aarch64
2021-05-16 22:00:39 +02:00