diff options
Diffstat (limited to 'pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh')
-rwxr-xr-x | pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh | 115 |
1 files changed, 80 insertions, 35 deletions
diff --git a/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh b/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh index fb7c8b2322a7a..54e9e92bb292c 100755 --- a/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh +++ b/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh @@ -34,6 +34,9 @@ targetHost= remoteSudo= verboseScript= noFlake= +attr= +buildFile=default.nix +buildingAttribute=1 installBootloader= json= @@ -58,6 +61,24 @@ while [ "$#" -gt 0 ]; do if [ -n "$action" ]; then showSyntax; fi action="$i" ;; + --file|-f) + if [ -z "$1" ]; then + log "$0: '$i' requires an argument" + exit 1 + fi + buildFile="$1" + buildingAttribute= + shift 1 + ;; + --attr|-A) + if [ -z "$1" ]; then + log "$0: '$i' requires an argument" + exit 1 + fi + attr="$1" + buildingAttribute= + shift 1 + ;; --install-grub) log "$0: --install-grub deprecated, use --install-bootloader instead" installBootloader=1 @@ -160,10 +181,6 @@ while [ "$#" -gt 0 ]; do esac done -if [[ -n "$SUDO_USER" ]]; then - useSudo=1 -fi - # log the given argument to stderr if verbose mode is on logVerbose() { if [ -n "$verboseScript" ]; then @@ -345,6 +362,12 @@ if [[ "$action" = switch || "$action" = boot || "$action" = test ]]; then canRun=1 fi +# Verify that user is not trying to use attribute building and flake +# at the same time +if [[ -z $buildingAttribute && -n $flake ]]; then + log "error: '--flake' cannot be used with '--file' or '--attr'" + exit 1 +fi # If ‘--upgrade’ or `--upgrade-all` is given, # run ‘nix-channel --update nixos’. @@ -426,7 +449,10 @@ trap cleanup EXIT # Re-execute nixos-rebuild from the Nixpkgs tree. if [[ -z $_NIXOS_REBUILD_REEXEC && -n $canRun && -z $fast ]]; then - if [[ -z $flake ]]; then + if [[ -z $buildingAttribute ]]; then + p=$(runCmd nix-build --no-out-link $buildFile -A "${attr:+$attr.}config.system.build.nixos-rebuild" "${extraBuildFlags[@]}") + SHOULD_REEXEC=1 + elif [[ -z $flake ]]; then if p=$(runCmd nix-build --no-out-link --expr 'with import <nixpkgs/nixos> {}; config.system.build.nixos-rebuild' "${extraBuildFlags[@]}"); then SHOULD_REEXEC=1 fi @@ -448,7 +474,10 @@ fi # Find configuration.nix and open editor instead of building. if [ "$action" = edit ]; then - if [[ -z $flake ]]; then + if [[ -z $buildingAttribute ]]; then + log "error: '--file' and '--attr' are not supported with 'edit'" + exit 1 + elif [[ -z $flake ]]; then NIXOS_CONFIG=${NIXOS_CONFIG:-$(runCmd nix-instantiate --find-file nixos-config)} if [[ -d $NIXOS_CONFIG ]]; then NIXOS_CONFIG=$NIXOS_CONFIG/default.nix @@ -490,31 +519,38 @@ prebuiltNix() { fi } -if [[ -n $buildNix && -z $flake ]]; then - log "building Nix..." +getNixDrv() { nixDrv= - if ! nixDrv="$(runCmd nix-instantiate '<nixpkgs/nixos>' --add-root "$tmpDir/nix.drv" --indirect -A config.nix.package.out "${extraBuildFlags[@]}")"; then - if ! nixDrv="$(runCmd nix-instantiate '<nixpkgs>' --add-root "$tmpDir/nix.drv" --indirect -A nix "${extraBuildFlags[@]}")"; then - if ! nixStorePath="$(runCmd nix-instantiate --eval '<nixpkgs/nixos/modules/installer/tools/nix-fallback-paths.nix>' -A "$(nixSystem)" | sed -e 's/^"//' -e 's/"$//')"; then - nixStorePath="$(prebuiltNix "$(uname -m)")" - fi - if ! runCmd nix-store -r "$nixStorePath" --add-root "${tmpDir}/nix" --indirect \ - --option extra-binary-caches https://cache.nixos.org/; then - log "warning: don't know how to get latest Nix" - fi - # Older version of nix-store -r don't support --add-root. - [ -e "$tmpDir/nix" ] || ln -sf "$nixStorePath" "$tmpDir/nix" - if [ -n "$buildHost" ]; then - remoteNixStorePath="$(runCmd prebuiltNix "$(buildHostCmd uname -m)")" - remoteNix="$remoteNixStorePath/bin" - if ! buildHostCmd nix-store -r "$remoteNixStorePath" \ - --option extra-binary-caches https://cache.nixos.org/ >/dev/null; then - remoteNix= - log "warning: don't know how to get latest Nix" - fi - fi + + if [[ -z $buildingAttribute ]]; then + if nixDrv="$(runCmd nix-instantiate $buildFile --add-root "$tmpDir/nix.drv" --indirect -A ${attr:+$attr.}config.nix.package.out "${extraBuildFlags[@]}")"; then return; fi + fi + if nixDrv="$(runCmd nix-instantiate '<nixpkgs/nixos>' --add-root "$tmpDir/nix.drv" --indirect -A config.nix.package.out "${extraBuildFlags[@]}")"; then return; fi + if nixDrv="$(runCmd nix-instantiate '<nixpkgs>' --add-root "$tmpDir/nix.drv" --indirect -A nix "${extraBuildFlags[@]}")"; then return; fi + + if ! nixStorePath="$(runCmd nix-instantiate --eval '<nixpkgs/nixos/modules/installer/tools/nix-fallback-paths.nix>' -A "$(nixSystem)" | sed -e 's/^"//' -e 's/"$//')"; then + nixStorePath="$(prebuiltNix "$(uname -m)")" + fi + if ! runCmd nix-store -r "$nixStorePath" --add-root "${tmpDir}/nix" --indirect \ + --option extra-binary-caches https://cache.nixos.org/; then + log "warning: don't know how to get latest Nix" + fi + # Older version of nix-store -r don't support --add-root. + [ -e "$tmpDir/nix" ] || ln -sf "$nixStorePath" "$tmpDir/nix" + if [ -n "$buildHost" ]; then + remoteNixStorePath="$(runCmd prebuiltNix "$(buildHostCmd uname -m)")" + remoteNix="$remoteNixStorePath/bin" + if ! buildHostCmd nix-store -r "$remoteNixStorePath" \ + --option extra-binary-caches https://cache.nixos.org/ >/dev/null; then + remoteNix= + log "warning: don't know how to get latest Nix" fi fi +} + +if [[ -n $buildNix && -z $flake ]]; then + log "building Nix..." + getNixDrv if [ -a "$nixDrv" ]; then nix-store -r "$nixDrv"'!'"out" --add-root "$tmpDir/nix" --indirect >/dev/null if [ -n "$buildHost" ]; then @@ -549,7 +585,9 @@ if [ "$action" = repl ]; then # This is a very end user command, implemented using sub-optimal means. # You should feel free to improve its behavior, as well as resolve tech # debt in "breaking" ways. Humans adapt quite well. - if [[ -z $flake ]]; then + if [[ -z $buildingAttribute ]]; then + exec nix repl --file $buildFile $attr "${extraBuildFlags[@]}" + elif [[ -z $flake ]]; then exec nix repl '<nixpkgs/nixos>' "${extraBuildFlags[@]}" else if [[ -n "${lockFlags[0]}" ]]; then @@ -687,8 +725,7 @@ EOF .nixosVersion, .kernelVersion, .configurationRevision, (.specialisations | join(" ")) ] | @tsv' | - column --separator $'\t' --table --table-columns "Generation,Build-date,NixOS version,Kernel,Configuration Revision,Specialisation" | - ${PAGER:cat} + column --separator $'\t' --table --table-columns "Generation,Build-date,NixOS version,Kernel,Configuration Revision,Specialisation" else jq --slurp . fi @@ -702,7 +739,9 @@ fi if [ -z "$rollback" ]; then log "building the system configuration..." if [[ "$action" = switch || "$action" = boot ]]; then - if [[ -z $flake ]]; then + if [[ -z $buildingAttribute ]]; then + pathToConfig="$(nixBuild $buildFile -A "${attr:+$attr.}config.system.build.toplevel" "${extraBuildFlags[@]}")" + elif [[ -z $flake ]]; then pathToConfig="$(nixBuild '<nixpkgs/nixos>' --no-out-link -A system "${extraBuildFlags[@]}")" else pathToConfig="$(nixFlakeBuild "$flake#$flakeAttr.config.system.build.toplevel" "${extraBuildFlags[@]}" "${lockFlags[@]}")" @@ -710,19 +749,25 @@ if [ -z "$rollback" ]; then copyToTarget "$pathToConfig" targetHostSudoCmd nix-env -p "$profile" --set "$pathToConfig" elif [[ "$action" = test || "$action" = build || "$action" = dry-build || "$action" = dry-activate ]]; then - if [[ -z $flake ]]; then + if [[ -z $buildingAttribute ]]; then + pathToConfig="$(nixBuild $buildFile -A "${attr:+$attr.}config.system.build.toplevel" "${extraBuildFlags[@]}")" + elif [[ -z $flake ]]; then pathToConfig="$(nixBuild '<nixpkgs/nixos>' -A system -k "${extraBuildFlags[@]}")" else pathToConfig="$(nixFlakeBuild "$flake#$flakeAttr.config.system.build.toplevel" "${extraBuildFlags[@]}" "${lockFlags[@]}")" fi elif [ "$action" = build-vm ]; then - if [[ -z $flake ]]; then + if [[ -z $buildingAttribute ]]; then + pathToConfig="$(nixBuild $buildFile -A "${attr:+$attr.}config.system.build.vm" "${extraBuildFlags[@]}")" + elif [[ -z $flake ]]; then pathToConfig="$(nixBuild '<nixpkgs/nixos>' -A vm -k "${extraBuildFlags[@]}")" else pathToConfig="$(nixFlakeBuild "$flake#$flakeAttr.config.system.build.vm" "${extraBuildFlags[@]}" "${lockFlags[@]}")" fi elif [ "$action" = build-vm-with-bootloader ]; then - if [[ -z $flake ]]; then + if [[ -z $buildingAttribute ]]; then + pathToConfig="$(nixBuild $buildFile -A "${attr:+$attr.}config.system.build.vmWithBootLoader" "${extraBuildFlags[@]}")" + elif [[ -z $flake ]]; then pathToConfig="$(nixBuild '<nixpkgs/nixos>' -A vmWithBootLoader -k "${extraBuildFlags[@]}")" else pathToConfig="$(nixFlakeBuild "$flake#$flakeAttr.config.system.build.vmWithBootLoader" "${extraBuildFlags[@]}" "${lockFlags[@]}")" |