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.
This commit is contained in:
Michal Sojka 2022-03-04 18:53:07 +01:00 committed by Profpatsch
parent 6396adbdfa
commit 611e9302f9
2 changed files with 23 additions and 1 deletions

View File

@ -91,6 +91,10 @@
<option>--flake</option> <replaceable>flake-uri</replaceable>
</arg>
<arg>
<option>--no-flake</option>
</arg>
<arg>
<option>--override-input</option> <replaceable>input-name</replaceable> <replaceable>flake-uri</replaceable>
</arg>
@ -594,6 +598,20 @@
</listitem>
</varlistentry>
<varlistentry>
<term>
<option>--no-flake</option>
</term>
<listitem>
<para>
Do not imply <option>--flake</option> if
<filename>/etc/nixos/flake.nix</filename> exists. With this
option, it is possible to build non-flake NixOS configurations
even if the current NixOS systems uses flakes.
</para>
</listitem>
</varlistentry>
</variablelist>
<para>

View File

@ -32,6 +32,7 @@ buildHost=localhost
targetHost=
remoteSudo=
verboseScript=
noFlake=
# comma separated list of vars to preserve when using sudo
preservedSudoVars=NIXOS_INSTALL_BOOTLOADER
@ -115,6 +116,9 @@ while [ "$#" -gt 0 ]; do
flakeFlags=(--extra-experimental-features 'nix-command flakes')
shift 1
;;
--no-flake)
noFlake=1
;;
--recreate-lock-file|--no-update-lock-file|--no-write-lock-file|--no-registries|--commit-lock-file)
lockFlags+=("$i")
;;
@ -339,7 +343,7 @@ fi
# Use /etc/nixos/flake.nix if it exists. It can be a symlink to the
# actual flake.
if [[ -z $flake && -e /etc/nixos/flake.nix ]]; then
if [[ -z $flake && -e /etc/nixos/flake.nix && -z $noFlake ]]; then
flake="$(dirname "$(readlink -f /etc/nixos/flake.nix)")"
fi