diff options
Diffstat (limited to '')
| -rwxr-xr-x | scripts/build.sh | 124 |
1 files changed, 78 insertions, 46 deletions
diff --git a/scripts/build.sh b/scripts/build.sh index d6a81754..9cc4e46b 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -1,45 +1,15 @@ -#!/usr/bin/env sh - -# nixos-config - My current NixOS configuration -# -# Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de> -# SPDX-License-Identifier: GPL-3.0-or-later -# -# This file is part of my nixos-config. -# -# You should have received a copy of the License along with this program. -# If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>. - -host="${1-$(hostname)}" -[ "$#" -gt 0 ] && shift 1 - -root="$(git rev-parse --show-toplevel)" - -SYSTEM_OUT="$root/result-system" -HOME_OUT="$root/result-home-soispha" - -check() { - file="$1" - if [ -s "$file" ]; then - rm "$file" - elif ! [ -e "$file" ]; then - rm "$file" || { - : "Ignore not existing files" - } - else - echo "ERROR: '$file' is not a symlink. Not removing it." 1>&2 - exit 1 - fi -} +#! /usr/bin/env sh +set -eu build_system() { - if [ -n "$BUILD_PAR" ]; then - max_jobs="8" - else - max_jobs="1" + host="$1" + + show_trace_arg="" + if [ "$show_trace" = true ]; then + show_trace_arg="--show-trace" fi - _val="$(nix build ".#nixosConfigurations.$host.config.system.build.toplevel" --print-out-paths --no-link --option max-jobs "$max_jobs" "$@")" + _val="$(nix build ".#nixosConfigurations.$host.config.system.build.toplevel" --print-out-paths --no-link --option max-jobs 1 $show_trace_arg)" exit_val="$?" if [ "$exit_val" -ne 0 ]; then @@ -49,17 +19,79 @@ build_system() { printf "%s" "$_val" fi } +remove_symlink() { + link="$1" + if [ -s "$link" ]; then + rm "$link" + elif ! [ -e "$link" ]; then + rm "$link" || { + : "Ignore not existing files" + } + else + echo "ERROR: '$link' is not a symlink. Not removing it." 1>&2 + exit 1 + fi +} -system="$(build_system "$@")" -[ "$system" = "1" ] && exit 1 +root="$(git rev-parse --show-toplevel)" +use_dirty=true +show_trace=false -check "$SYSTEM_OUT" -ln --symbolic "$system" "$SYSTEM_OUT" +while [ "$#" -ne 0 ]; do + case "$1" in + --dirty) + use_dirty=true + ;; + --clean) + use_dirty=false + ;; + --show-trace) + show_trace=true + ;; + --* | -*) + echo "arg '$1' is not known." + exit 2 + ;; + *) + # We reached the hosts + break + ;; + esac + shift 1 +done -home="$(grep ExecStart= "$SYSTEM_OUT/etc/systemd/system/home-manager-soispha.service" | awk '{print $2}')" -check "$HOME_OUT" -ln --symbolic "$home" "$HOME_OUT" +if [ "$use_dirty" = "false" ]; then + tmp="$(mktemp -d -t "server_build_script_XXXXX")" + trap 'rm -r "$tmp"' EXIT -nvd diff /run/current-system "$SYSTEM_OUT" + git archive --format=tar.gz HEAD | { + cd "$tmp" + tar -xz + } + cd "$tmp" +fi + +for host in "$@"; do + system="$(build_system "$host")" + [ "$system" = "1" ] && exit 1 + + result_link="$root/result-$host" + remove_symlink "$result_link" + nix-store --add-root "$result_link" --realise "$(readlink -f "$system")" + + # Try to also provide an easy link for potential home manager outputs. + fd "home-manager-\w*.service" "$result_link/etc/systemd/system" | while read -r hm; do + home_generation_link="$(grep ExecStart= "$hm" | awk '{print $2}')" + user_name="$(basename "$hm" | sed 's/home-manager-\(.*\)\.service/\1/')" + home_link="$root/result-$host-hm-$user_name" + + remove_symlink "$home_link" + ln --symbolic "$home_generation_link" "$home_link" + done + + if [ "$host" = "$(hostname)" ]; then + nvd diff /run/current-system "$result_link" + fi +done # vim: ft=sh |
