summaryrefslogtreecommitdiff
path: root/flake.nix
blob: 429b61c1d8a930517aaf94cd23c28c9e5229adfb (plain)
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
{
  description = "Check the forecast for today's Nix builds";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    nix-filter.url = "github:numtide/nix-filter";
  };

  outputs =
    {
      self,
      nixpkgs,
      nix-filter,
    }:
    let
      inherit (nixpkgs) lib;

      # We want to generate outputs for as many systems as possible,
      # even if we don't officially support or test for them
      allSystems = lib.systems.flakeExposed;

      # These are the systems we do officially support and test, though
      supportedSystems = [
        "x86_64-linux"
        "aarch64-linux"
        "x86_64-darwin"
        "aarch64-darwin"
      ];

      forAllSystems = lib.genAttrs allSystems;
      nixpkgsFor = forAllSystems (system: nixpkgs.legacyPackages.${system});
    in
    {
      checks = forAllSystems (
        system:
        let
          pkgs = nixpkgsFor.${system};
          packages = self.packages.${system};
        in
        lib.optionalAttrs (lib.elem system supportedSystems) {
          version-test = packages.nix-forecast.tests.version;

          clippy = packages.nix-forecast.overrideAttrs (oldAttrs: {
            pname = "check-clippy";

            nativeBuildInputs = oldAttrs.nativeBuildInputs ++ [
              pkgs.clippy
              pkgs.clippy-sarif
              pkgs.sarif-fmt
            ];

            buildPhase = ''
              runHook preBuild
              cargo clippy \
                --all-features \
                --all-targets \
                --tests \
                --message-format=json \
              | clippy-sarif | tee $out | sarif-fmt
            '';

            dontInstall = true;
            doCheck = false;
            dontFixup = true;

            passthru = { };
            meta = { };
          });

          formatting =
            pkgs.runCommand "check-formatting"
              {
                nativeBuildInputs = [
                  pkgs.cargo
                  pkgs.nixfmt-rfc-style
                  pkgs.rustfmt
                ];
              }
              ''
                cd ${self}

                echo "Running cargo fmt"
                cargo fmt -- --check

                echo "Running nixfmt..."
                nixfmt --check  .

                touch $out
              '';
        }
      );

      devShells = forAllSystems (
        system:
        let
          pkgs = nixpkgsFor.${system};
        in
        lib.optionalAttrs (lib.elem system supportedSystems) {
          default = pkgs.mkShell {
            packages = [
              # Rust tools
              pkgs.clippy
              pkgs.rust-analyzer
              pkgs.rustfmt

              # Nix tools
              self.formatter.${system}
              pkgs.nil
              pkgs.statix
            ];

            env = {
              RUST_SRC_PATH = toString pkgs.rustPlatform.rustLibSrc;
            };

            inputsFrom = [ self.packages.${system}.nix-forecast ];
          };
        }
      );

      formatter = forAllSystems (system: nixpkgsFor.${system}.nixfmt-rfc-style);

      # for CI
      legacyPackages = forAllSystems (
        system:
        lib.optionalAttrs (lib.elem system supportedSystems) (
          lib.mapAttrs' (name: lib.nameValuePair "check-${name}") self.checks.${system}
        )
      );

      packages = forAllSystems (
        system:
        let
          pkgs = nixpkgsFor.${system};
          nixForecastPackages = lib.makeScope pkgs.newScope (final: self.overlays.default final pkgs);

          isCompatible = lib.meta.availableOn pkgs.stdenv.hostPlatform;
          filteredPackages = lib.filterAttrs (
            _: deriv: isCompatible deriv && lib.isDerivation deriv
          ) nixForecastPackages;
        in
        filteredPackages
        // {
          default = filteredPackages.nix-forecast or pkgs.emptyFile;
        }
      );

      overlays.default = final: _: {
        nix-forecast = final.callPackage (
          {
            lib,
            stdenv,
            rustPlatform,
            darwin,
            installShellFiles,
            makeBinaryWrapper,
            nix,
            nix-forecast,
            testers,
          }:

          rustPlatform.buildRustPackage rec {
            pname = "nix-forecast";
            inherit (passthru.cargoTOML.package) version;

            cargoLock.lockFile = ./Cargo.lock;

            src = nix-filter {
              root = self;
              include = [
                ./Cargo.toml
                ./Cargo.lock
                ./build.rs
                "src"
              ];
            };

            nativeBuildInputs = [
              installShellFiles
              makeBinaryWrapper
            ];

            buildInputs = lib.optionals stdenv.isDarwin [
              darwin.apple_sdk.frameworks.CoreFoundation
              darwin.apple_sdk.frameworks.SystemConfiguration
              darwin.libiconv
            ];

            postInstall = ''
              wrapProgram $out/bin/nix-forecast --suffix PATH : "${lib.makeBinPath [ nix ]}"

              installShellCompletion --cmd nix-forecast \
                --bash completions/nix-forecast.bash \
              	--fish completions/nix-forecast.fish \
              	--zsh completions/_nix-forecast
            '';

            env = {
              COMPLETION_DIR = "completions";
            };

            passthru = {
              cargoTOML = lib.importTOML ./Cargo.toml;

              tests.version = testers.testVersion { package = nix-forecast; };
            };

            meta = {
              description = "Check the forecast for today's Nix builds";
              homepage = "https://github.com/getchoo/nix-forecast";
              changelog = "https://github.com/getchoo/nix-forecast/releases/tag/${version}";
              license = lib.licenses.mpl20;
              maintainers = with lib.maintainers; [ getchoo ];
              mainProgram = "nix-forecast";
            };
          }
        ) { };
      };
    };
}