-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
218 lines (174 loc) · 6.64 KB
/
flake.nix
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
{
description = "sodi flake";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
nixpkgs-stable.url = "github:NixOS/nixpkgs/nixos-24.05";
home-manager.url = "github:nix-community/home-manager";
home-manager.inputs.nixpkgs.follows = "nixpkgs";
nixos-hardware.url = "github:NixOS/nixos-hardware";
flashrom-meson.url = "github:roger/flashrom-meson-nix";
flashrom-meson.flake = false;
nix-monitored.url = "github:ners/nix-monitored";
sops-nix.url = "github:Mic92/sops-nix";
stylix.url = "github:danth/stylix";
nix-index-database.url = "github:nix-community/nix-index-database";
nix-index-database.inputs.nixpkgs.follows = "nixpkgs";
vscode-server.url = "github:nix-community/nixos-vscode-server";
nari.url = "github:sodiboo/nixos-razer-nari";
niri.url = "github:sodiboo/niri-flake";
niri-working-tree.url = "github:sodiboo/niri/support-lan-mouse";
niri-working-tree.flake = false;
nixpkgs-wayland.url = "github:nix-community/nixpkgs-wayland";
lan-mouse.url = "github:feschber/lan-mouse";
conduwuit.url = "github:girlbossceo/conduwuit";
sodipkgs-simutrans.url = "github:sodiboo/nixpkgs/simutrans";
sodipkgs-stackblur-go.url = "github:sodiboo/nixpkgs/stackblur";
picocss.url = "github:picocss/pico";
picocss.flake = false;
nixpkgs-with-meilisearch-at-1-8-3.url = "github:NixOS/nixpkgs/6edd5cc7bd8eb73d2e7c2f05b34c04a7a4d02de9";
zen-browser.url = "github:0xc000022070/zen-browser-flake";
zen-browser.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = {
self,
nixpkgs,
...
} @ inputs:
with nixpkgs.lib; let
match = flip getAttr;
read_dir_recursively = dir:
concatMapAttrs (this:
match {
directory = mapAttrs' (subpath: nameValuePair "${this}/${subpath}") (read_dir_recursively "${dir}/${this}");
regular = {
${this} = "${dir}/${this}";
};
symlink = {};
}) (builtins.readDir dir);
# `const` helper function is used extensively: the function is constant in regards to the name of the attribute.
params =
inputs
// {
configs = raw_configs;
elements = {
nitrogen = 7;
oxygen = 8;
sodium = 11;
iridium = 77;
};
inherit merge extras;
};
# It is important to note, that when adding a new `.mod.nix` file, you need to run `git add` on the file.
# If you don't, the file will not be included in the flake, and the modules defined within will not be loaded.
read_all_modules = flip pipe [
read_dir_recursively
(filterAttrs (flip (const (hasSuffix ".mod.nix"))))
(mapAttrs (const import))
(mapAttrs (const (flip toFunction params)))
];
merge = prev: this:
{
modules = prev.modules or [] ++ this.modules or [];
home_modules = prev.home_modules or [] ++ this.home_modules or [];
}
// (optionalAttrs (prev ? system || this ? system) {
system = prev.system or this.system;
});
all_modules = attrValues (read_all_modules "${self}");
raw_configs' = builtins.zipAttrsWith (machine:
if machine == "extras"
then mergeAttrsList
else builtins.foldl' merge {})
all_modules;
raw_configs = builtins.removeAttrs raw_configs' ["extras"];
extras = raw_configs'.extras or {};
configs = builtins.mapAttrs (const (config:
nixpkgs.lib.nixosSystem {
inherit (config) system;
modules =
config.modules
++ [
{
_module.args.home_modules = config.home_modules;
}
];
}))
raw_configs;
vms =
builtins.mapAttrs (hostname: {
config,
pkgs,
...
}: (
let
ssh = pkgs.writeScript "vm-ssh" ''
# do not verify the host key, do not store the host key, do not show a warning about the host key
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR \
-o User=sodiboo localhost -p $SSH_VM_PORT "$@"
'';
exit = pkgs.writeScript "vm-exit" ''
${ssh} /run/wrappers/bin/sudo /run/current-system/sw/bin/systemctl poweroff
'';
in
pkgs.writeShellScript "${hostname}-vm" ''
export SSH_VM_PORT=''${SSH_VM_PORT:-60022}
THIS_PID=$$
VM_TEMP_DIR=$(mktemp -d /tmp/${hostname}-XXXXXX)
cd $VM_TEMP_DIR
echo
echo
echo 'Starting VM...'
echo
QEMU_NET_OPTS="hostfwd=tcp::$SSH_VM_PORT-:22" ${config.virtualisation.vmVariant.system.build.vm}/bin/run-${hostname}-vm &
QEMU_PID=$!
monitor() {
tail --pid=$QEMU_PID -f /dev/null
kill $THIS_PID
}
cleanup() {
echo
echo
echo "The VM has exited. You can now have your normal shell back."
echo
rm -rf $VM_TEMP_DIR
exit
}
trap cleanup EXIT
monitor &
MONITOR_PID=$!
export ssh="${ssh}"
export exit="${exit}"
sleep 1
while kill -0 $MONITOR_PID 2>/dev/null; do
echo "Use $(tput setaf 2)\$ssh$(tput sgr0) to execute commands in the VM"
echo "Use $(tput setaf 1)\$exit$(tput sgr0) to power off the VM"
echo "Press $(tput setaf 4)Ctrl+D$(tput sgr0) to see this message again"
$SHELL
done
''
)) {
inherit (configs) sodium nitrogen;
};
in {
# for use in nix repl
p = s: builtins.trace "\n\n${s}\n" "---";
formatter.x86_64-linux = nixpkgs.legacyPackages.x86_64-linux.alejandra;
nixosConfigurations = builtins.mapAttrs (name: const configs.${name}) params.elements;
apps.x86_64-linux =
builtins.mapAttrs (name: script: {
type = "app";
program = "${script}";
})
vms;
# This is useful to rebuild all systems at once, for substitution
all-systems = nixpkgs.legacyPackages.x86_64-linux.runCommand "all-systems" {} (''
mkdir $out
''
+ (builtins.concatStringsSep "\n" (mapAttrsToList (
name: config: ''
ln -s ${config.config.system.build.toplevel} $out/${name}
''
)
self.nixosConfigurations)));
};
}