/ modules / flake / template.nix
template.nix
  1  # Allow using this repo in `nix flake init`
  2  { inputs, ... }:
  3  {
  4    flake = rec {
  5      templates =
  6        let
  7          mkDescription = name:
  8            "A ${name} template providing useful tools & settings for Nix-based development";
  9  
 10          filters = path: with inputs.nixpkgs.lib; rec {
 11            homeOnly =
 12              # NOTE: configurations/home/* is imported in nix-darwin and NixOS
 13              hasSuffix "activate-home.nix" path;
 14            darwinOnly =
 15              hasInfix "configurations/darwin" path
 16              || hasInfix "modules/darwin" path;
 17            nixosOnly =
 18              hasInfix "configurations/nixos" path
 19              || (hasInfix "modules/nixos/" path && !hasInfix "modules/nixos/common" path);
 20            homeFilter =
 21                !(nixosOnly || darwinOnly) && 
 22                !hasInfix "modules/nixos" path;
 23            alwaysExclude =
 24              hasSuffix "LICENSE" path
 25              || hasSuffix "README.md" path
 26              || hasInfix ".github" path
 27              || hasSuffix "template.nix" path
 28              || hasSuffix "test.nix" path
 29            ;
 30          };
 31        in
 32        {
 33          default = {
 34            description = mkDescription "nix-darwin/home-manager";
 35  
 36            path = builtins.path {
 37              path = inputs.self;
 38              filter = path: _:
 39                !(filters path).alwaysExclude;
 40            };
 41          };
 42  
 43          home = let parent = templates.default; in {
 44            description = mkDescription "home-manager";
 45            welcomeText = ''
 46              You have just created a nixos-unified-template flake.nix using home-manager.
 47  
 48              - Edit `./modules/home/*.nix` to customize your configuration.
 49              - Run `nix run` to apply the configuration.
 50              - Then, open a new terminal to see your new shell.
 51  
 52              Enjoy!
 53            '';
 54            path = builtins.path {
 55              path = parent.path;
 56              filter = path: _:
 57                let f = filters path;
 58                in
 59                  f.homeFilter;
 60            };
 61          };
 62  
 63          nixos = let parent = templates.default; in {
 64            description = mkDescription "NixOS";
 65            welcomeText = ''
 66              You have just created a nixos-unified-template flake.nix using NixOS.
 67  
 68              - Edit `./modules/nixos/*.nix` to customize your configuration.
 69              - Run `mv /etc/nixos/*.nix ./configurations/nixos/HOSTNAME/` to import your existing configuration.
 70              - Run `nix --extra-experimental-features "nix-command flakes" run` to apply the configuration.
 71  
 72              Enjoy!
 73            '';
 74            path = builtins.path {
 75              path = parent.path;
 76              filter = path: _:
 77                let f = filters path;
 78                in
 79                  !(f.darwinOnly || f.homeOnly);
 80            };
 81          };
 82  
 83          nix-darwin = let parent = templates.default; in {
 84            description = mkDescription "nix-darwin";
 85            welcomeText = ''
 86              You have just created a nixos-unified-template flake.nix using nix-darwin / home-manager.
 87  
 88              - Edit `./modules/{home,darwin}/*.nix` to customize your configuration.
 89  
 90              Then, as first-time activation, run:
 91  
 92              ```
 93              sudo mv /etc/nix/nix.conf /etc/nix/nix.conf.before-nix-darwin
 94              nix --extra-experimental-features "nix-command flakes" run
 95              ```
 96  
 97              Then, open a new terminal to see your new shell.
 98  
 99              Thereon, you can simply `nix run` whenever changing your configuration.
100  
101              Enjoy!
102            '';
103            path = builtins.path {
104              path = parent.path;
105              filter = path: _:
106                let f = filters path;
107                in
108                  !(f.nixosOnly || f.homeOnly);
109            };
110          };
111        };
112  
113      # https://omnix.page/om/init.html#spec
114      om.templates = {
115        home = {
116          template = templates.home;
117          params = [
118            {
119              name = "username";
120              description = "Your username as shown by `id -un`";
121              placeholder = "runner";
122            }
123            # Git
124            {
125              name = "git-name";
126              description = "Your full name for use in Git config";
127              placeholder = "John Doe";
128            }
129            {
130              name = "git-email";
131              description = "Your email for use in Git config";
132              placeholder = "johndoe@example.com";
133            }
134            # Neovim
135            {
136              name = "neovim";
137              description = "Include Neovim configuration";
138              paths = [ "**/neovim**" ];
139              value = false;
140            }
141          ];
142          tests = {
143            default = {
144              params = {
145                username = "john";
146                git-email = "john@ex.com";
147                git-name = "John Doe";
148                neovim = true;
149              };
150              asserts = {
151                source = {
152                  "modules/home/neovim/default.nix" = true;
153                  ".github/workflows" = false;
154                };
155                packages."homeConfigurations.john.activationPackage" = {
156                  "home-path/bin/nvim" = true;
157                  "home-path/bin/git" = true;
158                  "home-path/bin/om" = true;
159                  "home-files/.config/git/config" = true;
160                };
161              };
162            };
163          };
164        };
165  
166        nixos = {
167          template = templates.nixos;
168          params = [
169            {
170              name = "hostname";
171              description = "Your system hostname as shown by `hostname -s`";
172              placeholder = "example";
173            }
174          ] ++ om.templates.home.params;
175          tests = {
176            default = {
177              systems = [ "x86_64-linux" "aarch64-linux" ];
178              params = om.templates.home.tests.default.params // {
179                hostname = "example";
180              };
181              asserts = {
182                source = { };
183                packages."nixosConfigurations.example.config.system.build.toplevel" = {
184                  "etc/profiles/per-user/john/bin/git" = true;
185                };
186              };
187            };
188          };
189        };
190  
191        darwin = {
192          template = templates.nix-darwin;
193          params = [
194            {
195              name = "hostname";
196              description = "Your system hostname as shown by `hostname -s`";
197              placeholder = "example";
198            }
199          ] ++ om.templates.home.params;
200          tests = {
201            default = {
202              systems = [ "x86_64-darwin" "aarch64-darwin" ];
203              params = om.templates.home.tests.default.params // {
204                hostname = "example";
205              };
206              asserts = {
207                source = { };
208                packages."darwinConfigurations.example.config.system.build.toplevel" = {
209                  "etc/profiles/per-user/john/bin/git" = true;
210                };
211              };
212            };
213          };
214        };
215      };
216    };
217  }