diff options
author | Azat Bahawi <azat@bahawi.net> | 2022-08-12 22:53:53 +0300 |
---|---|---|
committer | Azat Bahawi <azat@bahawi.net> | 2022-08-12 22:53:53 +0300 |
commit | 61b94f0dd06cac0f7dcd38cce80f2a7ab8376098 (patch) | |
tree | b703a9fe11eae6c4cae4e4682f02caf0caa171d0 /packages/nixfiles.nix |
2022-08-12
Diffstat (limited to '')
-rw-r--r-- | packages/nixfiles.nix | 82 |
1 files changed, 82 insertions, 0 deletions
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 + ''; +} |