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