]> Git — Sourcephile - julm/julm-nix.git/blob - flake.nix
oignon: nftables: output-net: tcp dport 20022 accept
[julm/julm-nix.git] / flake.nix
1 {
2 inputs = {
3 doom-emacs.flake = false;
4 doom-emacs.url = "github:hlissner/doom-emacs";
5 home-manager.inputs.nixpkgs.follows = "nixpkgs";
6 home-manager.url = "github:nix-community/home-manager/release-23.05";
7 nix-formatter-pack.inputs.nixpkgs.follows = "nixpkgs";
8 nix-formatter-pack.url = "github:Gerschtli/nix-formatter-pack";
9 nixpkgs.url = "github:NixOS/nixpkgs/release-23.05";
10 pre-commit-hooks.inputs.nixpkgs.follows = "nixpkgs";
11 pre-commit-hooks.inputs.nixpkgs-stable.follows = "nixpkgs";
12 pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix";
13 };
14
15 outputs = inputs:
16 let
17 remoteNixpkgsPatches = import nixpkgs/patches.nix;
18 localNixpkgsPatches = [
19 ];
20 originPkgs = inputs.nixpkgs.legacyPackages."x86_64-linux";
21 nixpkgsPath = originPkgs.applyPatches {
22 name = "nixpkgs-patched";
23 src = inputs.nixpkgs.outPath;
24 patches = map originPkgs.fetchpatch remoteNixpkgsPatches ++ localNixpkgsPatches;
25 postPatch = ''
26 patch=$(printf '%s\n' ${builtins.concatStringsSep " "
27 (map (p: p.sha256) remoteNixpkgsPatches ++ localNixpkgsPatches)} |
28 sort | sha256sum | cut -c -7)
29 echo "-patch-$patch" >.version-suffix
30 '';
31 };
32 profile = "/nix/var/nix/profiles/system";
33 inherit (inputs.nixpkgs) lib;
34 #nixosSystem = lib.nixosSystem;
35 nixosSystem = import (nixpkgsPath + "/nixos/lib/eval-config.nix");
36 forAllSystems = f: lib.genAttrs lib.systems.flakeExposed (system: f rec {
37 inherit system;
38 #pkgs = inputs.nixpkgs.legacyPackages.${system};
39 pkgs = import nixpkgsPath {
40 inherit system;
41 overlays = import nixpkgs/overlays.nix;
42 };
43 });
44 in
45 {
46 # nix -L build .#hello
47 packages = forAllSystems ({pkgs, ...}: pkgs);
48
49 # nix -L build .#nixosConfigurations.oignon.config.system.build.toplevel
50 # nix -L build .#nixosConfigurations.oignon.config.boot.kernelPackages.kernel.configfile
51 # nix -L build .#nixosConfigurations.oignon.pkgs.hello
52 # nix eval --raw .#nixosConfigurations.oignon.config.networking.nftables.rulese
53 nixosConfigurations = lib.genAttrs
54 (builtins.attrNames (lib.filterAttrs (_n: v: v == "directory") (builtins.readDir (inputs.self + "/hosts"))))
55 (hostName: nixosSystem {
56 system = null;
57 specialArgs = {
58 # Required to avoid infinite recursion
59 # when `inputs` is used in `imports`.
60 inherit inputs;
61 };
62 modules = [
63 nixos/default.nix
64 (import (./hosts + "/${hostName}.nix"))
65 {
66 _module.args = {
67 inherit hostName;
68 hosts = inputs.self.nixosConfigurations;
69 host = inputs.self.nixosConfigurations.${hostName}._module.args;
70 };
71 nixpkgs.overlays = import nixpkgs/overlays.nix;
72 nixpkgs.config.allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [ "hplip" "geogebra" ];
73 }
74 inputs.home-manager.nixosModules.home-manager
75 {
76 home-manager.useGlobalPkgs = true;
77 home-manager.useUserPackages = true;
78 home-manager.verbose = true;
79 home-manager.backupFileExtension = null;
80 home-manager.extraSpecialArgs = {
81 inherit hostName inputs;
82 };
83 }
84 ];
85 });
86
87 # nix -L develop or direnv allow
88 devShell = forAllSystems ({ pkgs, system, ... }: pkgs.callPackage ./shell.nix
89 {
90 inherit pkgs inputs system nixpkgsPath;
91 inherit (inputs.self.checks.${system}.pre-commit-check) shellHook;
92 });
93
94 # nix -L run .#oignon.switch
95 apps = forAllSystems ({ pkgs, system, ... }:
96 with builtins;
97 mapAttrs
98 (hostName: { config, ... }:
99 let
100 inherit (config.system) build;
101 target = "\"\${TARGET:-root@${config.networking.hostName}.${config.networking.domain}}\"";
102 scriptApp = scriptName: ps: script: {
103 type = "app";
104 program = (pkgs.writeShellScript "${hostName}-${scriptName}" ''
105 export PATH="${lib.makeBinPath ([ pkgs.coreutils ] ++ ps)}:$PATH"
106 set -eux
107 ${script}
108 '').outPath;
109 };
110 in
111 {
112 # Example: nix run .#aubergine.switch
113 "switch" = scriptApp "switch" [ ] ''
114 shopt -s globstar
115
116 chmod -R g-rwx,o-rwx **/*.gpg
117 trap 'git reset **/*.gpg' EXIT
118 git rm -rf --cached --ignore-unmatch **/*.gpg # prevent copying to /nix/store
119
120 nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
121 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
122
123 nix copy --to ssh://${target}${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
124
125 if ssh ${target} set -x ';' \
126 systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
127 test "''${NO_NIXOS_FALLBACK:+set}" '||' \
128 systemd-run -u nixos-fallback --description=nixos-fallback /bin/sh -xc '''\'''
129 PATH=${with pkgs; lib.makeBinPath [ coreutils nix systemd ]}
130 sleep $((10 * 60))
131 ${profile}/bin/switch-to-configuration switch
132 systemctl reboot
133 '\'''' '&&' \
134 ${build.toplevel}/bin/switch-to-configuration test
135 then
136 ssh ${target} -o ControlPath=none set -x ';' \
137 systemctl stop nixos-fallback.service ';' \
138 nix-env --profile ${profile} --set '${build.toplevel}' ';' \
139 ${build.toplevel}/bin/switch-to-configuration boot '&&' \
140 nix-env --delete-generations 7d --profile ${profile}
141 else
142 tput rev
143 echo WARNING: switch-to-configuration was not registered at boot
144 tput sgr0
145 ssh ${target} -o ControlPath=none set -x ';' \
146 systemctl stop nixos-fallback.service
147 false
148 fi
149 '';
150 })
151 inputs.self.nixosConfigurations);
152
153 # nix flake check
154 checks = forAllSystems (args: with args; {
155 pre-commit-check = inputs.pre-commit-hooks.lib.${system}.run {
156 src = inputs.self;
157 hooks = {
158 nixpkgs-fmt.enable = true;
159 };
160 };
161 });
162
163 # nix fmt
164 formatter = forAllSystems ({ pkgs, ... }:
165 inputs.nix-formatter-pack.lib.mkFormatter {
166 inherit pkgs;
167 config = {
168 tools = {
169 deadnix.enable = true;
170 nixpkgs-fmt.enable = true;
171 statix.enable = true;
172 };
173 };
174 }
175 );
176 };
177 }