aboutsummaryrefslogtreecommitdiff
path: root/flake.nix
blob: 557e5503296fa3a0ce271d6fa7a5c051015a4329 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
{
  description = "personal NixOS configurations";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-25.05";

    nixpkgsUnstable.url = "github:nixos/nixpkgs/nixos-unstable";

    home-manager = {
      url = "github:nix-community/home-manager/release-25.05";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    darwin = {
      url = "github:lnl7/nix-darwin/nix-darwin-25.05";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    disko = {
      url = "github:nix-community/disko";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    agenix = {
      url = "github:ryantm/agenix";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    treefmt-nix = {
      url = "github:numtide/treefmt-nix";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    pre-commit-hooks = {
      url = "github:cachix/git-hooks.nix";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    emacs-overlay = {
      url = "github:nix-community/emacs-overlay";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  # Output config, or config for NixOS system
  outputs =
    {
      self,
      nixpkgs,
      nixpkgsUnstable,
      darwin,
      treefmt-nix,
      pre-commit-hooks,
      emacs-overlay,
      agenix,
      ...
    }@inputs:
    let
      supportedSystems = [
        "aarch64-darwin"
        "x86_64-linux"
      ];

      # Function to generate attributes for each system
      forAllSystems = nixpkgs.lib.genAttrs supportedSystems;

      # Function to get pkgs for a specific system
      getPkgs =
        system:
        import nixpkgs {
          inherit system;
          config.allowUnfree = true;
          overlays = overlays;
        };

      getPkgsUnstable =
        system:
        import nixpkgsUnstable {
          inherit system;
        };

      # Define overlays here
      overlays = [
        emacs-overlay.overlay
        (final: _prev: {
          # Load all packages from the pkgs directory
          customPackages = builtins.mapAttrs (
            name: _:
            final.callPackage (./pkgs + "/${name}") {
              pkgsUnstable = getPkgsUnstable final.system;
            }
          ) (builtins.readDir ./pkgs);
        })
      ];

      mkSystem = import ./nix/lib/mkSystem.nix {
        inherit
          self
          nixpkgs
          inputs
          overlays
          ;
      };

      mkFcunyNet =
        system:
        let
          pkgs = getPkgs system;
        in
        import ./src/fcuny.net { inherit pkgs; };

      # Create a treefmt-nix evaluation for a system
      mkTreefmtEval =
        system:
        let
          pkgs = getPkgs system;
        in
        treefmt-nix.lib.evalModule pkgs {
          projectRootFile = "flake.nix";
          programs = {
            nixfmt.enable = true;
            deadnix.enable = true;
          };
        };

      # Create pre-commit hooks for a system and source
      mkPreCommitHooks =
        system: src:
        let
          treefmtEval = mkTreefmtEval system;
        in
        inputs.pre-commit-hooks.lib.${system}.run {
          inherit src;
          hooks = {
            check-merge-conflicts.enable = true;
            deadnix.enable = true;
            detect-private-keys.enable = true;
            end-of-file-fixer.enable = true;
            mixed-line-endings.enable = true;
            shellcheck = {
              enable = true;
              excludes = [ "\\.envrc$" ];
            };
            flake-checker.enable = true;
            treefmt = {
              enable = true;
              entry = "${treefmtEval.config.build.wrapper}/bin/treefmt --ci";
            };
            trim-trailing-whitespace.enable = true;
          };
        };
    in
    {
      nixosModules = import ./nix/modules;

      packages = forAllSystems (
        system:
        let
          fcunyNet = mkFcunyNet system;
        in
        {
          "fcuny_net" = fcunyNet.site;
        }
      );

      apps = forAllSystems (
        system:
        let
          fcunyNet = mkFcunyNet system;
        in
        {
          "fcuny_net-serve" = {
            type = "app";
            program = "${fcunyNet.serve}/bin/serve-fcuny-net";
          };
        }
      );

      # nix fmt
      formatter = forAllSystems (
        system:
        let
          treefmtEval = mkTreefmtEval system;
        in
        treefmtEval.config.build.wrapper
      );

      # nix flake check
      checks = forAllSystems (system: {
        pre-commit-check = mkPreCommitHooks system ./.;
      });

      # my VM running on the synology NAS
      nixosConfigurations.vm-synology = mkSystem "vm-synology" {
        system = "x86_64-linux";
        user = "fcuny";
      };

      # my personal MacBook Air
      darwinConfigurations.mba-m2 = mkSystem "mba-m2" {
        system = "aarch64-darwin";
        user = "fcuny";
        darwin = true;
      };

      # work laptop
      darwinConfigurations.HQ-KWNY2VH41P = mkSystem "hq-kwny2vh41p" {
        system = "aarch64-darwin";
        user = "fcuny";
        darwin = true;
      };

      # Dev shells for each system
      devShells = forAllSystems (
        system:
        let
          pkgs = getPkgs system;
          pre-commit-check = mkPreCommitHooks system ./.;
          scripts = import ./nix/scripts {
            inherit pkgs system inputs;
          };
        in
        {
          default = pkgs.mkShellNoCC {
            inherit (pre-commit-check) shellHook; # This is the key line
            packages =
              with pkgs;
              [
                nixos-rebuild
                git
                inputs.agenix.packages."${system}".default
              ]
              ++ scripts.all;
          };
        }
      );
    };
}