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