]> Git — Sourcephile - julm/julm-nix.git/blob - flake.nix
fcitx5: enable chinese
[julm/julm-nix.git] / flake.nix
1 {
2 description = "julm's Nix configurations for hosts (NixOS) and homes (home-manager)";
3 /*
4 nixConfig = {
5 extra-substituters = [
6 "https://nix-community.cachix.org"
7 ];
8 extra-trusted-public-keys = [
9 "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="
10 ];
11 };
12 */
13
14 inputs = {
15 doom-emacs.flake = false;
16 doom-emacs.url = "github:hlissner/doom-emacs";
17 git-hooks.inputs.nixpkgs.follows = "nixpkgs";
18 git-hooks.url = "github:cachix/git-hooks.nix";
19 home-manager.inputs.nixpkgs.follows = "nixpkgs";
20 home-manager.url = "github:nix-community/home-manager/release-24.11";
21 nixos-hardware.url = "github:NixOS/nixos-hardware/master";
22 nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
23 };
24
25 outputs =
26 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' ${
38 builtins.concatStringsSep " " (map (p: p.sha256) remoteNixpkgsPatches ++ localNixpkgsPatches)
39 } |
40 sort | sha256sum | cut -c -7)
41 echo "-patch-$patch" >.version-suffix
42 '';
43 };
44 profile = "/nix/var/nix/profiles/system";
45 inherit (inputs.nixpkgs) lib;
46 overlays =
47 system:
48 import nixpkgs/overlays.nix
49 ++ [
50 ];
51 #nixosSystem = lib.nixosSystem;
52 nixosSystem = import (nixpkgsPath + "/nixos/lib/eval-config.nix");
53 forAllSystems =
54 f:
55 lib.genAttrs lib.systems.flakeExposed (
56 system:
57 f rec {
58 inherit system;
59 #pkgs = inputs.nixpkgs.legacyPackages.${system};
60 pkgs = import nixpkgsPath {
61 inherit system;
62 overlays = overlays system;
63 };
64 }
65 );
66 self =
67 with lib.fileset;
68 toSource {
69 root = ./.;
70 fileset =
71 let
72 exts = [
73 "clear"
74 "conf"
75 "cred"
76 "crt"
77 "css"
78 "el"
79 "hs"
80 "json"
81 "lua"
82 "nix"
83 "patch"
84 "pem"
85 "pub"
86 "sh"
87 "theme"
88 "vim"
89 ];
90 in
91 unions [
92 (fileFilter (file: lib.any file.hasExt exts) ./domains)
93 (fileFilter (file: lib.any file.hasExt exts) ./home-manager)
94 (fileFilter (file: lib.any file.hasExt exts) ./homes)
95 (fileFilter (file: lib.any file.hasExt exts) ./hosts)
96 (fileFilter (file: lib.any file.hasExt exts) ./nixos)
97 (fileFilter (file: lib.any file.hasExt exts) ./nixpkgs)
98 (fileFilter (file: lib.any file.hasExt exts) ./users)
99 ./shell.nix
100 ];
101 };
102 in
103 rec {
104 # nix -L build .#hello
105 packages = forAllSystems ({ pkgs, ... }: pkgs);
106
107 # nix -L build .#nixosConfigurations.oignon.config.system.build.toplevel
108 # nix -L build .#nixosConfigurations.oignon.config.boot.kernelPackages.kernel.configfile
109 # nix -L build .#nixosConfigurations.oignon.pkgs.hello
110 # nix eval --raw .#nixosConfigurations.oignon.config.networking.nftables.ruleset
111 nixosConfigurations =
112 lib.genAttrs
113 (builtins.attrNames (
114 lib.filterAttrs (_n: v: v == "directory") (builtins.readDir (self + "/hosts"))
115 ))
116 (
117 hostName:
118 nixosSystem {
119 system = null;
120 specialArgs = {
121 # Required to avoid infinite recursion
122 # when `inputs` is used in `imports`.
123 inherit inputs;
124 };
125 modules = [
126 nixos/default.nix
127 (import (self + "/hosts/${hostName}.nix"))
128 (
129 { config, ... }:
130 {
131 _module.args = {
132 inherit hostName;
133 hosts = nixosConfigurations;
134 host = nixosConfigurations.${hostName}._module.args;
135 };
136 nixpkgs.overlays = overlays config.nixpkgs.hostPlatform.system;
137 nixpkgs.config.permittedInsecurePackages = [
138 # Still needed for chatty
139 # See https://github.com/NixOS/nixpkgs/pull/334638#issuecomment-2289025802%3E
140 # and https://gitlab.gnome.org/World/Chatty/-/issues/932
141 "olm-3.2.16"
142 ];
143 nixpkgs.config.allowUnfreePredicate =
144 pkg:
145 builtins.elem (lib.getName pkg) [
146 "anydesk"
147 "canon-cups-ufr2"
148 "cudatoolkit"
149 "geogebra"
150 "hplip"
151 "memtest86-efi"
152 "nvidia-settings"
153 "nvidia-x11"
154 ];
155 }
156 )
157 inputs.home-manager.nixosModules.home-manager
158 {
159 home-manager.useGlobalPkgs = true;
160 home-manager.useUserPackages = true;
161 home-manager.verbose = true;
162 #home-manager.force = true;
163 home-manager.backupFileExtension = "old";
164 home-manager.extraSpecialArgs = {
165 inherit hostName inputs;
166 };
167 }
168 ];
169 }
170 );
171
172 # nix -L develop or direnv allow
173 devShells = forAllSystems (
174 { pkgs, system, ... }:
175 {
176 default = pkgs.callPackage (self + "/shell.nix") {
177 inherit
178 pkgs
179 inputs
180 system
181 nixpkgsPath
182 ;
183 inherit (checks.${system}.git-hooks-check) shellHook;
184 };
185 }
186 );
187
188 # nix -L run .#oignon.switch
189 apps = forAllSystems (
190 { pkgs, system, ... }:
191 with builtins;
192 mapAttrs (
193 hostName:
194 { config, ... }:
195 let
196 inherit (config.system) build;
197 scriptApp = scriptName: ps: script: {
198 type = "app";
199 program =
200 (pkgs.writeShellScript "${hostName}-${scriptName}" ''
201 export PATH="${lib.makeBinPath ([ pkgs.coreutils ] ++ ps)}:$PATH"
202 set -eux
203 ${script}
204 '').outPath;
205 };
206 in
207 {
208 # Example: nix run .#aubergine.switch
209 "switch" = scriptApp "switch" [ ] ''
210 shopt -s globstar
211
212 chmod -R g-rwx,o-rwx **/*.gpg
213 trap 'git reset **/*.gpg' EXIT
214 git rm -rf --cached --ignore-unmatch **/*.gpg # prevent copying to /nix/store
215
216 nix-store --add-root hosts/${hostName}.nixpkgs --indirect --realise ${nixpkgsPath}
217 nix-store --add-root hosts/${hostName}.root --indirect --realise ${build.toplevel}
218
219 nix copy --to "ssh://${config.install.target}?''${targetStore-}"${lib.optionalString config.install.substituteOnDestination " --substitute-on-destination"} ${build.toplevel}
220
221 if ssh ${config.install.target} set -x ';' \
222 systemctl reset-failed nixos-fallback '2>/dev/null' ';' \
223 test "''${NO_NIXOS_FALLBACK:+set}" '||' \
224 systemd-run -u nixos-fallback --description=nixos-fallback /bin/sh -xc '''\'''
225 PATH=${
226 with pkgs;
227 lib.makeBinPath [
228 coreutils
229 nix
230 systemd
231 ]
232 }
233 sleep $((10 * 60))
234 ${profile}/bin/switch-to-configuration switch
235 systemctl reboot
236 '\'''' '&&' \
237 ${build.toplevel}/bin/switch-to-configuration test
238 then
239 ssh ${config.install.target} -o ControlPath=none set -x ';' \
240 systemctl stop nixos-fallback.service ';' \
241 nix-env --profile ${profile} --set '${build.toplevel}' ';' \
242 ${build.toplevel}/bin/switch-to-configuration boot '&&' \
243 nix-env --delete-generations 7d --profile ${profile}
244 else
245 tput rev
246 echo WARNING: switch-to-configuration was not registered at boot
247 tput sgr0
248 ssh ${config.install.target} -o ControlPath=none set -x ';' \
249 systemctl stop nixos-fallback.service
250 false
251 fi
252 '';
253 }
254 ) nixosConfigurations
255 );
256
257 # nix flake check
258 checks = forAllSystems (
259 args: with args; {
260 git-hooks-check = inputs.git-hooks.lib.${system}.run {
261 src = self;
262 hooks = {
263 nixfmt-rfc-style.enable = true;
264 };
265 };
266 }
267 );
268 };
269 }