blob: 3f17edc497591516764e3088ffd677921d98372d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
{
git,
jq,
nix,
openssh,
symlinkJoin,
writeShellApplication,
writeTextDir,
xdg-utils,
}: let
bin = writeShellApplication {
name = "nixfiles";
runtimeInputs = [
git
jq
nix
openssh
xdg-utils
];
# Shamelessly appropriated from https://github.com/ncfavier/config.
# Hopefully Naïm will not sue me for copyright infrigment.
text = ''
nixfiles="''${NIXFILES:-$HOME/src/nixfiles}"
cmd=$1
shift
case $cmd in
repl|eval|build|rev)
args=() flakeArgs=()
for arg do case $arg in
-w|--wip) flakeArgs+=(--override-flake nixfiles "$nixfiles");;
*) args+=("$arg")
esac done
set -- "''${args[@]}"
;;&
compare)
input=$1
# shellcheck disable=SC1090
. <(nix flake metadata --json nixfiles | jq -r --arg input "$input" '
def browse($url): @sh "xdg-open \($url)";
.locks.nodes[$input] |
if .locked.type == "github" then
browse("https://github.com/\(.locked.owner)/\(.locked.repo)/compare/\(.locked.rev)...\(.original.ref // "master")")
elif .locked.type == "gitlab" then
browse("https://gitlab.com/\(.locked.owner)/\(.locked.repo)/-/compare/\(.locked.rev)...\(.original.ref // "master")")
else
"echo unsupported input type \(.locked.type) (supported: github, gitlab)"
end
')
;;
update)
if (( $# )); then
args=()
for input do args+=(--update-input "$input"); done
exec nix flake lock --refresh "''${args[@]}" "$nixfiles"
else
# https://github.com/NixOS/nix/issues/6095
exec nix flake update -v --refresh "$nixfiles"
fi
;;
rev)
if (( $# )); then
expr=inputs.''${1//'/'/.inputs.}.rev
else
expr=self.revision
fi
nix eval "''${flakeArgs[@]}" -f ~/.nix-defexpr --raw "$expr"
echo
;;
repl)
exec nix repl "''${flakeArgs[@]}" -f ~/.nix-defexpr "$@";;
eval)
exec nix eval "''${flakeArgs[@]}" -f ~/.nix-defexpr --json "$@" | jq -r .;;
build)
# https://github.com/NixOS/nix/issues/6661
exec nix-build --log-format bar-with-logs "''${flakeArgs[@]}" ~/.nix-defexpr -A "$@";;
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;;
home)
attr=nixosConfigurations."$HOSTNAME".config.hm.home.activationPackage
export VERBOSE=1
exec nix shell -v ".#$attr" "$@" -c home-manager-generation;;
@*)
host=''${cmd#@}
hostname=$(ssh -q "$host" 'echo "$HOSTNAME"')
exec nixos-rebuild -v --flake ".#$hostname" --target-host "$host" --use-remote-sudo "$@";;
*)
exec nixos-rebuild -v --fast --flake "." --use-remote-sudo "$cmd" "$@";;
esac
'';
};
bashCompletion = writeTextDir "/share/bash-completion/completions/nixfiles" ''
__compreply() {
local completion
while IFS= read -r completion; do
COMPREPLY+=("$completion")
done < <(compgen "$@" -- "$cur")
}
__complete_nix_cmd() {
local skip=$1; shift
COMP_WORDS=("$@" "''${COMP_WORDS[@]:skip}")
(( COMP_CWORD += $# - skip ))
_completion_loader nix
_complete_nix
}
_nixfiles() {
local cur prev words cword
local nixfiles="''${NIXFILES:-$HOME/src/nixfiles}"
_init_completion -n ':=&'
if [[ "$cword" == 1 ]] || [[ "$cword" == 2 && "$prev" == @* ]]; then
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'
fi
else case ''${words[1]} in
compare|update|rev)
__complete_nix_cmd "$cword" nix flake lock "$nixfiles" --update-input
;;
repl|eval|bld)
compreply -W '-w --wip'
;;&
repl)
__complete_nix_cmd 2 nix repl ~/.nix-defexpr
;;
eval)
__complete_nix_cmd 2 nix eval -f ~/.nix-defexpr --json
;;
build)
__complete_nix_cmd 2 nix build -f ~/.nix-defexpr --json
;;
home)
__complete_nix_cmd 2 nix shell "$nixfiles"
;;
build|switch)
__complete_nix_cmd 2 nix build "$nixfiles"
;;
esac fi
}
complete -F _nixfiles nixfiles
'';
in
symlinkJoin {
name = "nixfiles";
paths = [bin bashCompletion];
}
|