Unverified Commit b52dc66e authored by jopejoe1's avatar jopejoe1 Committed by GitHub
Browse files

nixos-{rebuild,install}: use system.nix (#493229)

parents 169fd51a fadc273d
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
@@ -4,6 +4,32 @@

<!-- To avoid merge conflicts, consider adding your item at an arbitrary place in the list instead. -->

- The system.nix file has been added has an alternative entry point to configuration.nix (and flake.nix) that allows to configure NixOS without using `nix-channel`.
  This file must evaluate to a NixOS system derivation or an attribute set of such derivations, in which case the attribute to build has to be selected with the `--attr` option of `nixos-rebuild` or `nixos-install`.
  For example,
  ```nix
  # system.nix
  let
    # Pinned Nixpkgs archive
    #
    # Use `curl -I https://channels.nixos.org/nixos-26.05` to get the
    # latest commit of the stable channel and `nix-prefetch-url --unpack`
    # to compute its sha256 hash.
    nixpkgs = builtins.fetchTarball {
      url = "https://github.com/NixOS/nixpkgs/archive/c217913993d6.tar.gz";
      sha256 = "026mprs324330pfazlgbw987qmsa8ligglarvqbcxzig2kgw0lqg";
    };
  in
  import "${nixpkgs}/nixos" {
    # Build NixOS using an external configuration.nix file,
    # or directly set your options here
    configuration = ./configuration.nix;
  }
  ```

  The default location of system.nix is `/etc/nixos/system.nix` and can be changed by setting the `<nixos-system>` search path.
  `nixos-rebuild` and `nixos-install` can now also load a system.nix file in the current directory (only if `--attr` is used) or from a directory specified with `--file`.

- The default kernel package has been updated from 6.12 to 6.18. All supported kernels remain available.

## New Modules {#sec-release-26.05-new-modules}
+2 −2
Original line number Diff line number Diff line
@@ -56,9 +56,9 @@ in
          '';
        }
        ''
          nixos-install --help | grep -F 'NixOS Reference Pages'
          nixos-install --help | grep -F "System Manager's Manual"
          nixos-install --help | grep -F 'configuration.nix'
          nixos-generate-config --help | grep -F 'NixOS Reference Pages'
          nixos-generate-config --help | grep -F "System Manager's Manual"
          nixos-generate-config --help | grep -F 'hardware-configuration.nix'

          # FIXME: Tries to call unshare, which it must not do for --help
+22 −12
Original line number Diff line number Diff line
@@ -33,9 +33,19 @@
.Sh DESCRIPTION
This command installs NixOS in the file system mounted on
.Pa /mnt Ns
, based on the NixOS configuration specified in
.Pa /mnt/etc/nixos/configuration.nix Ns
\&. It performs the following steps:
, or defined through the
.Fl -root
option, based on the NixOS configuration specified in
.Pa /mnt/etc/nixos/system.nix Ns
,
.Pa /mnt/etc/nixos/configuration.nix
or specified through the
.Fl -file Ns
,
.Fl -attr Ns
or
.Fl -flake Ns
options. It performs the following steps:
.
.Bl -enum
.It
@@ -46,9 +56,7 @@ It copies Nix and its dependencies to
.It
It runs Nix in
.Pa /mnt
to build the NixOS configuration specified in
.Pa /mnt/etc/nixos/configuration.nix Ns
\&.
to build the given NixOS configuration.
.
.It
It installs the current channel
@@ -114,7 +122,7 @@ output named
\&.
.
.It Fl -file Ar path , Fl f Ar path
Enable and build the NixOS system from the specified file. The file must
Build the NixOS system from the specified file. The file must
evaluate to an attribute set, and it must contain a valid NixOS configuration
at attribute
.Va attrPath Ns
@@ -127,17 +135,19 @@ function in nixpkgs or importing and calling
from nixpkgs. If specified without
.Fl -attr
option, builds the configuration from the top-level
attribute of the file.
attribute set of the file.
.
.It Fl -attr Ar attrPath , Fl A Ar attrPath
Enable and build the NixOS system from nix file and use the specified attribute
Build the NixOS system from nix file and use the specified attribute
path from file specified by the
.Fl -file
option. If specified without
.Fl -file
option, uses
.Va [root] Ns Pa /etc/nixos/default.nix Ns
\&.
option, it tires to find
.Pa system.nix
in
.Va root Ns Pa /etc/nixos Ns
, in current directory and iteratively in parent directories.
.
.It Fl -channel Ar channel
If this option is provided, do not copy the current
+89 −26
Original line number Diff line number Diff line
@@ -19,7 +19,8 @@ system=
verbosity=()
attr=
buildFile=
buildingAttribute=1
# keys: module, by-attrset, flake, system
declare -A requestedBuildMethods

while [ "$#" -gt 0 ]; do
    i="$1"; shift 1
@@ -38,10 +39,12 @@ while [ "$#" -gt 0 ]; do
            ;;
        --system|--closure|--store-path)
            system="$1"; shift 1
            requestedBuildMethods["system"]=1
            ;;
        --flake)
          flake="$1"
          flakeFlags=(--extra-experimental-features 'nix-command flakes')
          requestedBuildMethods["flake"]=1
          shift 1
          ;;
        --file|-f)
@@ -49,8 +52,14 @@ while [ "$#" -gt 0 ]; do
                log "$0: '$i' requires an argument"
                exit 1
            fi
            if [ -f "$1" ]; then
                buildFile="$1"
            buildingAttribute=
            elif [ -f "$1/system.nix" ]; then
                buildFile="${1%/}/system.nix"
            else
                buildFile="${1%/}/default.nix"
            fi
            requestedBuildMethods["by-attrset"]=1
            shift 1
            ;;
        --attr|-A)
@@ -59,7 +68,7 @@ while [ "$#" -gt 0 ]; do
                exit 1
            fi
            attr="$1"
            buildingAttribute=
            requestedBuildMethods["by-attrset"]=1
            shift 1
            ;;
        --recreate-lock-file|--no-update-lock-file|--no-write-lock-file|--no-registries|--commit-lock-file)
@@ -106,6 +115,20 @@ while [ "$#" -gt 0 ]; do
    esac
done

# Finds a specific file in a directory or its parents
findInParents() {
    local dir=$1
    local filename=$2
    while [[ ! -f "$dir/$filename" ]] && [[ "$dir" != / ]]; do
        dir=$(dirname "$dir")
    done
    if [[ -f "$dir/$filename" ]]; then
        echo "$dir/$filename"
    else
        return 1
    fi
}

if ! test -e "$mountPoint"; then
    echo "mount point $mountPoint doesn't exist"
    exit 1
@@ -122,30 +145,61 @@ while [[ "$checkPath" != "/" ]]; do
    checkPath="$(dirname "$checkPath")"
done

# Verify that user is not trying to use attribute building and flake
# at the same time
if [[ -z $buildingAttribute && -n $flake ]]; then
    echo "$0: '--flake' cannot be used with '--file' or '--attr'"
# If user requested multiple build methods, abort
if [[ ${#requestedBuildMethods[@]} -gt 1 ]]; then
    echo "error: multiple build methods requested: ${!requestedBuildMethods[@]}"
    exit 1
fi

# Get the path of the NixOS configuration file.
if [[ -z $flake && -n $buildingAttribute ]]; then
    if [[ -z $NIXOS_CONFIG ]]; then
        NIXOS_CONFIG=$mountPoint/etc/nixos/configuration.nix
findByAttrset() {
    # - From system.nix up from the current directory
    # - Hardcoded to $mountPoint/etc/nixos/system.nix
    # - Hardcoded to /etc/nixos/system.nix
    # (default.nix is also searched for backward compatibility)
    if resolvedBuildFile="$(findInParents "$PWD" system.nix)"; then
        buildFile=$resolvedBuildFile
        return 0
    elif resolvedBuildFile="$(findInParents "$PWD" default.nix)"; then
        buildFile=$resolvedBuildFile
        return 0
    elif [[ -f "/etc/nixos/system.nix" ]]; then
        buildFile="/etc/nixos/system.nix"
        return 0
    elif [[ -f "/etc/nixos/default.nix" ]]; then
        buildFile="/etc/nixos/default.nix"
        return 0
    fi
    return 1
}

    if [[ ${NIXOS_CONFIG:0:1} != / ]]; then
        echo "$0: \$NIXOS_CONFIG is not an absolute path"
findModule() {
    # - From NIXOS_CONFIG environment variable
    # - hardcoded to $mountPoint/etc/nixos/configuration.nix
    if [[ -n $NIXOS_CONFIG ]]; then
        return 0
    elif [[ -f "$mountPoint/etc/nixos/configuration.nix" ]]; then
        NIXOS_CONFIG="$mountPoint/etc/nixos/configuration.nix"
        return 0
    fi
    return 1
}

# If user didn't request a specific build method, try to find one
if [[ ${#requestedBuildMethods[@]} -eq 0 ]]; then
    # Flakes cannot be resolved without knowing hostname
    if findByAttrset; then
        requestedBuildMethods["by-attrset"]=1
    elif findModule; then
        requestedBuildMethods["module"]=1
    else
        echo "error: no build method found"
        exit 1
    fi
elif [[ -z $buildingAttribute ]]; then
    if [[ -z $buildFile ]]; then
        buildFile="$mountPoint/etc/nixos/default.nix"
    elif [[ -d $buildFile ]]; then
        buildFile="$buildFile/default.nix"
fi
elif [[ -n $flake ]]; then

# Find configuration files if not already found
if [[ -n "${requestedBuildMethods["flake"]}" ]]; then
    if [[ $flake =~ ^(.*)\#([^\#\"]*)$ ]]; then
       flake="${BASH_REMATCH[1]}"
       flakeAttr="${BASH_REMATCH[2]}"
@@ -156,19 +210,28 @@ elif [[ -n $flake ]]; then
        exit 1
    fi
    flakeAttr="nixosConfigurations.\"$flakeAttr\""
elif [[ -n "${requestedBuildMethods["by-attrset"]}" && -z $buildFile ]]; then
    findByAttrset
elif [[ -n "${requestedBuildMethods["module"]}" && -z $NIXOS_CONFIG ]]; then
    findModule

    if [[ ${NIXOS_CONFIG:0:1} != / ]]; then
        echo "$0: \$NIXOS_CONFIG is not an absolute path"
        exit 1
    fi
fi

# Resolve the flake.
if [[ -n $flake ]]; then
if [[ -n "${requestedBuildMethods["flake"]}" ]]; then
    flake=$(nix "${flakeFlags[@]}" flake metadata --json "${extraBuildFlags[@]}" "${lockFlags[@]}" -- "$flake" | jq -r .url)
fi

if [[ ! -e $NIXOS_CONFIG && -z $system && -z $flake && -n $buildingAttribute ]]; then
if [[ ! -e $NIXOS_CONFIG && -n "${requestedBuildMethods["module"]}" ]]; then
    echo "configuration file $NIXOS_CONFIG doesn't exist"
    exit 1
fi

if [[ ! -z $buildingAttribute && -e $buildFile && -z $system ]]; then
if [[ ! -e $buildFile && -n "${requestedBuildMethods["by-attrset"]}" ]]; then
    echo "configuration file $buildFile doesn't exist"
    exit 1
fi
@@ -202,12 +265,12 @@ fi
# Build the system configuration in the target filesystem.
if [[ -z $system ]]; then
    outLink="$tmpdir/system"
    if [[ -z $flake && -n $buildingAttribute ]]; then
    if [[ -n "${requestedBuildMethods["module"]}" ]]; then
        echo "building the configuration in $NIXOS_CONFIG..."
        nix-build --out-link "$outLink" --store "$mountPoint" "${extraBuildFlags[@]}" \
            --extra-substituters "$sub" \
            '<nixpkgs/nixos>' -A system -I "nixos-config=$NIXOS_CONFIG" "${verbosity[@]}"
    elif [[ -z $buildingAttribute ]]; then
    elif [[ -n "${requestedBuildMethods["by-attrset"]}" ]]; then
        if [[ -n $attr ]]; then
            echo "building the configuration in $buildFile and attribute $attr..."
        else
@@ -216,7 +279,7 @@ if [[ -z $system ]]; then
        nix-build --out-link "$outLink" --store "$mountPoint" "${extraBuildFlags[@]}" \
            --extra-substituters "$sub" \
            "$buildFile" -A "${attr:+$attr.}config.system.build.toplevel" "${verbosity[@]}"
    else
    else # [[ -n "${requestedBuildMethods["flake"]}" ]]
        echo "building the flake in $flake..."
        nix "${flakeFlags[@]}" build "$flake#$flakeAttr.config.system.build.toplevel" \
            --store "$mountPoint" --extra-substituters "$sub" "${verbosity[@]}" \
+15 −8
Original line number Diff line number Diff line
@@ -28,11 +28,12 @@ _nixos-rebuild_ \[--verbose] [--quiet] [--max-jobs MAX_JOBS] [--cores CORES] [--
# DESCRIPTION

This command updates the system so that it corresponds to the configuration
specified in /etc/nixos/configuration.nix, /etc/nixos/flake.nix or the file and
attribute specified by the *--file* and/or *--attr* options. Thus, every
specified in /etc/nixos/configuration.nix, /etc/nixos/flake.nix,
/etc/nixos/system.nix or the file and attribute specified by the *--file*,
*--attr* or *--flake* options. Thus, every
time you modify the configuration or any other NixOS module, you must run
*nixos-rebuild* to make the changes take effect. It builds the new system in
/nix/store, runs its activation script, and stop and (re)starts any system
/nix/store, runs its activation script, stops and (re)starts any system
services if needed. Please note that user services need to be started manually
as they aren't detected by the activation script at the moment.

@@ -281,20 +282,21 @@ It must be one of the following:
	Implies *--sudo*.

*--file* _path_, *-f* _path_
	Enable and build the NixOS system from the specified file. The file must
	Build the NixOS system from the specified file. The file must
	evaluate to an attribute set, and it must contain a valid NixOS
	configuration at attribute _attrPath_. This is useful for building a
	NixOS system from a nix file that is not a flake or a NixOS
	configuration module. Attribute set a with valid NixOS configuration can
	be made using _nixos_ function in nixpkgs or importing and calling
	nixos/lib/eval-config.nix from nixpkgs. If specified without *--attr*
	option, builds the configuration from the top-level attribute of the
	option, builds the configuration from the top-level attribute set of the
	file.

*--attr* _attrPath_, *-A* _attrPath_
	Enable and build the NixOS system from nix file and use the specified
	attribute path from file specified by the *--file* option. If specified
	without *--file* option, uses _default.nix_ in current directory.
	Build the NixOS system from a nix file and use the specified
	attribute path from the file specified by the *--file* option.
	If specified without *--file* option, uses _system.nix_ in current directory,
	the system-wide _<nixos-system>_ file, or finally, /etc/nixos/system.nix.

*--flake* _flake-uri[#name]_, *-F* _flake-uri[#name]_
	Build the NixOS system from the specified flake. It defaults to the
@@ -355,6 +357,11 @@ NIX_SUDOOPTS

# FILES

/etc/nixos/system.nix
	If this file exists, then *nixos-rebuild* will use it as if the
	*--file* option was given. This allows to build a self-contained
	system configuration, without requiring nixos channel.

/etc/nixos/flake.nix
	If this file exists, then *nixos-rebuild* will use it as if the
	*--flake* option was given. This file may be a symlink to a
Loading