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