From 0221d5913ea26fde9493dadfbb265f2ff103124f Mon Sep 17 00:00:00 2001 From: Azat Bahawi Date: Sat, 30 Sep 2023 15:21:49 +0300 Subject: 2023-09-30 --- packages/nixfiles.nix | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) (limited to 'packages/nixfiles.nix') diff --git a/packages/nixfiles.nix b/packages/nixfiles.nix index 9b16e84..b534e3c 100644 --- a/packages/nixfiles.nix +++ b/packages/nixfiles.nix @@ -1,8 +1,10 @@ { git, jq, + lib, nix, openssh, + stdenv, symlinkJoin, writeShellApplication, writeTextDir, @@ -11,13 +13,14 @@ bin = writeShellApplication { name = "nixfiles"; - runtimeInputs = [ - git - jq - nix - openssh - xdg-utils - ]; + runtimeInputs = + [ + git + jq + nix + openssh + ] + ++ lib.optional (!stdenv.isDarwin) xdg-utils; # Shamelessly appropriated from https://github.com/ncfavier/config. # Hopefully Naïm will not sue me for copyright infrigment. @@ -26,7 +29,7 @@ cmd=$1 shift case $cmd in - repl|eval|build|rev) + repl|eval|nix-build|rev) args=() flakeArgs=() for arg do case $arg in -w|--wip) @@ -39,10 +42,15 @@ set -- "''${args[@]}" ;;& compare) + if command -v xdg-open >/dev/null; then + open="xdg-open" + else + open="open" + fi input=$1 # shellcheck disable=SC1090 - . <(nix flake metadata --json nixfiles | jq -r --arg input "$input" ' - def browse($url): @sh "xdg-open \($url)"; + . <(nix flake metadata --json nixfiles | jq -r --arg open "$open" --arg input "$input" ' + def browse($url): @sh "$open \($url)"; .locks.nodes[$input] | if .locked.type == "github" then browse("https://github.com/\(.locked.owner)/\(.locked.repo)/compare/\(.locked.rev)...\(.original.ref // "master")") @@ -78,13 +86,14 @@ eval) exec nix eval "''${flakeArgs[@]}" -f ~/.nix-defexpr --json "$@" | jq -r . ;; - build) + nix-build) # https://github.com/NixOS/nix/issues/6661 exec nix-build --log-format bar-with-logs "''${flakeArgs[@]}" ~/.nix-defexpr -A "$@" ;; specialise) name=$1 shift + # Assumes that sudo is already configured. Has setuid baked in if needed and etc. exec sudo /run/current-system/specialisation/"$name"/bin/switch-to-configuration switch ;; revert) @@ -96,6 +105,7 @@ exec nix shell -v ".#$attr" "$@" -c home-manager-generation ;; @*) + # Assumes that hosts are configured in the OpenSSH configuration. host=''${cmd#@} hostname=$(ssh -q "$host" 'echo "$HOSTNAME"') exec nixos-rebuild -v --flake ".#$hostname" --target-host "$host" --use-remote-sudo "$@" @@ -130,13 +140,13 @@ if [[ $cur == @* ]]; then _known_hosts_real -a -- "$cur" else - __compreply -W 'compare update repl eval bld specialise revert home build build-vm test switch boot' + __compreply -W 'compare update repl eval nix-build specialise revert home boot build build-vm build-vm-with-bootloader dry-activate dry-build edit switch test' fi else case ''${words[1]} in compare|update|rev) __complete_nix_cmd "$cword" nix flake lock "$nixfiles" --update-input ;; - repl|eval|bld) + repl|eval|nix-build) compreply -W '-w --wip' ;;& repl) @@ -151,7 +161,7 @@ home) __complete_nix_cmd 2 nix shell "$nixfiles" ;; - build|switch) + switch) __complete_nix_cmd 2 nix build "$nixfiles" ;; esac fi @@ -161,5 +171,13 @@ in symlinkJoin { name = "nixfiles"; + paths = [bin bashCompletion]; + + meta = with lib; { + description = "A helper utility to manage NixOS configurations with Nix flakes"; + homepage = "https://git.azahi.cc/nixfiles"; + license = licenses.wtfpl; + maintainers = with maintainers; [azahi]; + }; } -- cgit v1.2.3