-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
421 lines (348 loc) · 15.1 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
{
description = "Este é o nix (com flakes) para o ambiente de desenvolvimento do github-ci-runner";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs";
flake-utils.url = "github:numtide/flake-utils";
podman-rootless.url = "github:ES-Nix/podman-rootless/from-nixpkgs";
# sops-nix.url = "github:Mic92/sops-nix";
# sops-nix.inputs.nixpkgs.follows = "nixpkgs";
podman-rootless.inputs.nixpkgs.follows = "nixpkgs";
};
outputs =
allAttrs@{ self
, nixpkgs
, ...
}:
allAttrs.flake-utils.lib.eachDefaultSystem
(system:
let
name = "github-ci-runner";
pkgsAllowUnfree = import nixpkgs {
inherit system;
config = { allowUnfree = true; };
};
hack = pkgsAllowUnfree.writeShellScriptBin "hack" ''
# Dont overwrite customised configuration
# https://dev.to/ifenna__/adding-colors-to-bash-scripts-48g4
echo -e '\n\n\n\e[32m\tAmbiente pronto!\e[0m\n'
echo -e '\n\t\e[33mignore as proximas linhas...\e[0m\n\n\n'
'';
# https://gist.github.com/tpwrules/34db43e0e2e9d0b72d30534ad2cda66d#file-flake-nix-L28
pleaseKeepMyInputs = pkgsAllowUnfree.writeTextDir "bin/.please-keep-my-inputs"
(builtins.concatStringsSep " " (builtins.attrValues allAttrs));
in
rec {
packages.vm = self.nixosConfigurations.vm.config.system.build.toplevel;
/*
# Utilized by `nix run .#<name>`
rm -fv nixos.qcow2
nix run --impure --refresh --verbose .#vm
# Open the QMEU VM terminal and:
start-github-runner-with-pat "$PAT"
*/
apps.vm = {
type = "app";
program = "${self.nixosConfigurations.vm.config.system.build.vm}/bin/run-nixos-vm";
};
# nix fmt
formatter = pkgsAllowUnfree.nixpkgs-fmt;
devShells.default = pkgsAllowUnfree.mkShell {
buildInputs = with pkgsAllowUnfree; [
age
allAttrs.podman-rootless.packages.${system}.podman
bashInteractive
coreutils
curl
gettext
gnumake
hack
httpie
jq
patchelf
sops
ssh-to-age
];
shellHook = ''
# TODO: documentar esse comportamento,
# devo abrir issue no github do nixpkgs
export TMPDIR=/tmp
echo "Entering the nix devShell no github-ci-runner"
test -d .profiles || mkdir -v .profiles
test -L .profiles/dev \
|| nix develop .# --profile .profiles/dev --command true
test -L .profiles/dev-shell-default \
|| nix build $(nix eval --impure --raw .#devShells."$system".default.drvPath) --out-link .profiles/dev-shell-"$system"-default
test -L .profiles/nixosConfigurations."$system".vm.config.system.build.vm \
|| nix build --impure --out-link .profiles/nixosConfigurations."$system".vm.config.system.build.vm .#nixosConfigurations.vm.config.system.build.vm
# For SOPS
# test -d ~/.config/sops/age || mkdir -pv ~/.config/sops/age
# test -f ~/.config/sops/age/keys.txt || age-keygen -o ~/.config/sops/age/keys.txt
# https://github.com/getsops/sops/pull/860/files#diff-7b3ed02bc73dc06b7db906cf97aa91dec2b2eb21f2d92bc5caa761df5bbc168fR192
# test -d secrets || mkdir -v secrets
# test -f secrets/secrets.yaml.encrypted \
# || sops \
# --encrypt \
# --age $(age-keygen -y ~/.config/sops/age/keys.txt) \
# secrets/secrets.yaml > secrets/secrets.yaml.encrypted
hack
'';
};
})
// {
nixosConfigurations.vm = nixpkgs.lib.nixosSystem {
# About system and maybe --impure
# https://www.youtube.com/watch?v=90aB_usqatE&t=3483s
system = builtins.currentSystem;
modules = [
({ config, nixpkgs, pkgs, lib, modulesPath, ... }:
let
nixuserKeys = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExR+PSB/jBwJYKfpLN+MMXs3miRn70oELTV3sXdgzpr";
in
{
# Internationalisation options
i18n.defaultLocale = "en_US.UTF-8";
# i18n.defaultLocale = "pt_BR.UTF-8";
console.keyMap = "br-abnt2";
# Why
# nix flake show --impure .#
# break if it does not exists?
# Use systemd boot (EFI only)
boot.loader.systemd-boot.enable = true;
fileSystems."/" = { device = "/dev/hda1"; };
virtualisation.vmVariant = {
virtualisation.useNixStoreImage = false; # TODO: hardening
virtualisation.writableStore = true; # TODO: hardening
programs.dconf.enable = true;
# security.polkit.enable = true; # TODO: hardening?
virtualisation.memorySize = 1024 * 2; # Use MiB memory.
virtualisation.diskSize = 1024 * 16; # Use MiB memory.
virtualisation.cores = 8; # Number of cores.
virtualisation.graphics = true;
virtualisation.resolution = lib.mkForce { x = 1024; y = 768; };
virtualisation.qemu.options = [
# Better display option
# TODO: -display sdl,gl=on
# https://gitlab.com/qemu-project/qemu/-/issues/761
"-vga virtio"
"-display gtk,zoom-to-fit=false"
# Enable copy/paste
# https://www.kraxel.org/blog/2021/05/qemu-cut-paste/
"-chardev qemu-vdagent,id=ch1,name=vdagent,clipboard=on"
"-device virtio-serial-pci"
"-device virtserialport,chardev=ch1,id=ch1,name=com.redhat.spice.0"
];
};
users.users.root = {
password = "root";
initialPassword = "root";
openssh.authorizedKeys.keyFiles = [
"${ pkgs.writeText "nixuser-keys.pub" "${toString nixuserKeys}" }"
];
};
# https://nixos.wiki/wiki/NixOS:nixos-rebuild_build-vm
users.extraGroups.nixgroup.gid = 999;
security.sudo.wheelNeedsPassword = false; # TODO: hardening
users.users.nixuser = {
isSystemUser = true;
password = "101"; # TODO: hardening
createHome = true;
home = "/home/nixuser";
homeMode = "0700";
description = "The VM tester user";
group = "nixgroup";
extraGroups = [
"docker"
"kubernetes"
"kvm"
"libvirtd"
"nixgroup"
"podman"
"qemu-libvirtd"
"root"
"wheel"
];
packages = with pkgs; [
awscli
btop
coreutils
direnv
file
git
gnumake
openssh
starship
which
sops
github-runner
curl
jq
httpie
(
writeScriptBin "start-github-runner-with-pat" ''
#! ${pkgs.runtimeShell} -e
PAT="$1"
# https://github.com/actions/runner/issues/323#issuecomment-583640437
# https://github.com/actions/runner/issues/2538#issuecomment-1517988404
config.sh \
--ephemeral \
--pat "$PAT" \
--replace \
--runasservice \
--unattended \
--url https://github.com/imobanco/github-ci-runner \
&& run.sh
''
)
];
shell = pkgs.zsh;
uid = 1234;
autoSubUidGidRange = true;
openssh.authorizedKeys.keyFiles = [
"${ pkgs.writeText "nixuser-keys.pub" "${toString nixuserKeys}" }"
];
openssh.authorizedKeys.keys = [
"${toString nixuserKeys}"
];
};
# imports = [ allAttrs.sops-nix.nixosModules.sops ];
/*
https://github.com/NixOS/nixpkgs/issues/169812
https://github.com/actions/runner/issues/1882#issuecomment-1427930611
nix shell nixpkgs#github-runner --command \
sh \
-c \
'config.sh --url https://github.com/imobanco/github-ci-runner --pat "$PAT" --ephemeral && run.sh'
config.sh --url https://github.com/imobanco/github-ci-runner --pat "$PAT" --ephemeral && run.sh
TODO: https://www.youtube.com/watch?v=G5f6GC7SnhU
*/
# services.github-runner.enable = true;
# services.github-runner.url = "https://github.com/imobanco";
# services.github-runner.tokenFile = config.sops.secrets."github-runner/token".path;
# services.github-runner.extraPackages = with pkgs; [ config.virtualisation.docker.package ];
# virtualisation.docker.enable = true;
# systemd.services.github-runner.serviceConfig.SupplementaryGroups = [ "docker" ];
virtualisation.docker.enable = true;
/*
https://github.com/vimjoyer/sops-nix-video/tree/25e5698044e60841a14dcd64955da0b1b66957a2
https://github.com/Mic92/sops-nix/issues/65#issuecomment-929082304
https://discourse.nixos.org/t/qmenu-secrets-sops-and-nixos/13621/8
https://www.youtube.com/watch?v=1BquzE3Yb4I
https://github.com/FiloSottile/age#encrypting-to-a-github-user
https://devops.datenkollektiv.de/using-sops-with-age-and-git-like-a-pro.html
sudo cat /run/secrets/example-key
*/
/*
sops.defaultSopsFile = ./secrets/secrets.yaml.encrypted;
sops.defaultSopsFormat = "yaml";
sops.gnupg.sshKeyPaths = [];
sops.age.sshKeyPaths = [];
sops.age.keyFile = ./secrets/keys.txt;
sops.secrets.example-key = { };
*/
programs.zsh = {
enable = true;
enableCompletion = true;
autosuggestions.enable = true;
syntaxHighlighting.enable = true;
interactiveShellInit = ''
export ZSH=${pkgs.oh-my-zsh}/share/oh-my-zsh
export ZSH_THEME="agnoster"
export ZSH_CUSTOM=${pkgs.zsh-autosuggestions}/share/zsh-autosuggestions
plugins=(
colored-man-pages
docker
git
#zsh-autosuggestions # Why this causes an warn?
#zsh-syntax-highlighting
)
# https://nixos.wiki/wiki/Fzf
source $ZSH/oh-my-zsh.sh
export DIRENV_LOG_FORMAT=""
eval "$(direnv hook zsh)"
eval "$(starship init zsh)"
export FZF_BASE=$(fzf-share)
source "$(fzf-share)/completion.zsh"
source "$(fzf-share)/key-bindings.zsh"
'';
ohMyZsh.custom = "${pkgs.zsh-autosuggestions}/share/zsh-autosuggestions";
promptInit = "";
};
fonts = {
fontDir.enable = true;
fonts = with pkgs; [
powerline
powerline-fonts
];
enableDefaultFonts = true;
enableGhostscriptFonts = true;
};
# Enable ssh
services.sshd.enable = true;
# https://github.com/NixOS/nixpkgs/issues/21332#issuecomment-268730694
services.openssh = {
allowSFTP = true;
kbdInteractiveAuthentication = false;
enable = true;
forwardX11 = false;
passwordAuthentication = false;
permitRootLogin = "yes";
ports = [ 10022 ];
authorizedKeysFiles = [
"${ pkgs.writeText "nixuser-keys.pub" "${toString nixuserKeys}" }"
];
};
# Hack to fix annoying zsh warning, too overkill probably
# https://www.reddit.com/r/NixOS/comments/cg102t/how_to_run_a_shell_command_upon_startup/eudvtz1/?utm_source=reddit&utm_medium=web2x&context=3
systemd.user.services.fix-zsh-warning = {
script = ''
echo "Fixing a zsh warning"
# https://stackoverflow.com/questions/638975/how-wdo-i-tell-if-a-regular-file-does-not-exist-in-bash#comment25226870_638985
test -f /home/nixuser/.zshrc || touch /home/nixuser/.zshrc && chown nixuser: -Rv /home/nixuser
'';
wantedBy = [ "default.target" ];
};
# https://nixos.wiki/wiki/Libvirt
# https://discourse.nixos.org/t/set-up-vagrant-with-libvirt-qemu-kvm-on-nixos/14653
boot.extraModprobeConfig = "options kvm_intel nested=1";
# X configuration
services.xserver.enable = true;
services.xserver.layout = "br";
services.xserver.displayManager.autoLogin.user = "nixuser";
# https://nixos.org/manual/nixos/stable/#sec-xfce
services.xserver.desktopManager.xfce.enable = true;
services.xserver.desktopManager.xfce.enableScreensaver = false;
services.xserver.videoDrivers = [ "qxl" ];
# For copy/paste to work
services.spice-vdagentd.enable = true;
nixpkgs.config.allowUnfree = true;
nix = {
extraOptions = "experimental-features = nix-command flakes";
package = pkgs.nixVersions.nix_2_10;
readOnlyStore = true;
registry.nixpkgs.flake = nixpkgs; # https://bou.ke/blog/nix-tips/
nixPath = [
"nixpkgs=/etc/channels/nixpkgs"
"nixos-config=/etc/nixos/configuration.nix"
];
};
environment.etc."channels/nixpkgs".source = nixpkgs.outPath;
environment.systemPackages = with pkgs; [
bashInteractive
openssh
direnv
nix-direnv
fzf
neovim
nixos-option
oh-my-zsh
zsh
zsh-autosuggestions
zsh-completions
];
system.stateVersion = "22.11";
})
];
specialArgs = { inherit nixpkgs allAttrs; };
};
};
}