From 61b94f0dd06cac0f7dcd38cce80f2a7ab8376098 Mon Sep 17 00:00:00 2001 From: Azat Bahawi Date: Fri, 12 Aug 2022 22:53:53 +0300 Subject: 2022-08-12 --- packages/nixfiles.nix | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 packages/nixfiles.nix (limited to 'packages/nixfiles.nix') diff --git a/packages/nixfiles.nix b/packages/nixfiles.nix new file mode 100644 index 0000000..90a5865 --- /dev/null +++ b/packages/nixfiles.nix @@ -0,0 +1,82 @@ +{ + git, + jq, + lib, + nix, + nixfilesSrc ? "$HOME/src/nixfiles", + openssh, + writeShellApplication, +}: +writeShellApplication { + name = "nixfiles"; + runtimeInputs = [git jq nix openssh]; + text = '' + nixfiles=${lib.escapeShellArg nixfilesSrc} + cmd="$1" + shift + case $cmd in + update) + if (( $# )); then + args=() + for input do args+=(--update-input "$input"); done + exec nix flake lock "$nixfiles" "''${args[@]}" + else + exec nix flake update -v "$nixfiles" + fi + ;; + rev) + nix flake metadata --json nixfiles | + if (( $# )); then + jq -r --arg input "$1" '.locks.nodes[$input].locked.rev' + else + jq -r '.revision // "dirty"' + fi + ;; + repl|eval|build) + args=() + for arg do case $arg in + -w|--wip) args+=(--override-flake config "$nixfiles") + ;; + *) args+=("$arg") + esac done + set -- "''${args[@]}" + ;;& + repl) + exec nix repl ~/.nix-defexpr "$@" + ;; + eval) + exec nix eval -f ~/.nix-defexpr --json "$@" | jq -r . + ;; + build) + exec nix build -f ~/.nix-defexpr --json "$@" | jq -r . + ;; + home) + attr="nixosConfigurations.$HOSTNAME.config.hm.home.activationPackage" + export VERBOSE=1 + exec nix shell --verbose "$nixfiles#$attr" "$@" --command home-manager-generation + ;; + specialise) + name=$1 + shift + exec sudo /run/current-system/specialisation/"$name"/bin/switch-to-configuration switch + ;; + revert) + exec sudo /nix/var/nix/profiles/system/bin/switch-to-configuration switch + ;; + test-headless) + exec nixos-rebuild build-vm --verbose --print-build-logs --flake "$nixfiles#test-headless" "$@" + ;; + test-headful) + exec nixos-rebuild build-vm --verbose --print-build-logs --flake "$nixfiles#test-headful" "$@" + ;; + @*) + host="''${cmd#@}" + hostname="$(ssh -q "$host" 'echo "$HOSTNAME"')" + exec nixos-rebuild -v --flake "$nixfiles#$hostname" --target-host "$host" --use-remote-sudo "$@" + ;; + *) + exec nixos-rebuild -v --flake "$nixfiles" --use-remote-sudo "$cmd" "$@" + ;; + esac + ''; +} -- cgit v1.2.3