summaryrefslogtreecommitdiff
path: root/flake.nix
blob: 3cd44ec8a55b6f5fea3f4738d1eaa4161dbb49a5 (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
{
  description = "Userspace tools for bcachefs";

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

    flake-parts.url = "github:hercules-ci/flake-parts";

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

    crane = {
      url = "github:ipetkov/crane";
      inputs.nixpkgs.follows = "nixpkgs";
    };

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

    flake-compat = {
      url = "github:edolstra/flake-compat";
      flake = false;
    };
  };

  outputs =
    inputs@{
      self,
      nixpkgs,
      flake-parts,
      treefmt-nix,
      fenix,
      crane,
      ...
    }:
    flake-parts.lib.mkFlake { inherit inputs; } {
      imports = [ inputs.treefmt-nix.flakeModule ];

      # can be extended, but these have proper binary cache support in nixpkgs
      # as of writing.
      systems = [
        "aarch64-linux"
        "x86_64-linux"
      ];

      perSystem =
        {
          self',
          config,
          lib,
          pkgs,
          system,
          ...
        }:
        let
          cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
          rustfmtToml = builtins.fromTOML (builtins.readFile ./rustfmt.toml);

          craneLib = crane.mkLib pkgs;

          commit = lib.strings.substring 0 7 (builtins.readFile ./.bcachefs_revision);

          commonArgs = {
            version = "git-${commit}";
            src = self;

            makeFlags = [
              "DESTDIR=${placeholder "out"}"
              "PREFIX="
              "VERSION=${commit}"
            ];

            dontStrip = true;

            nativeBuildInputs = with pkgs; [
              pkg-config
              rustPlatform.bindgenHook
            ];

            buildInputs = with pkgs; [
              attr
              keyutils
              libaio
              libsodium
              liburcu
              libuuid
              lz4
              udev
              zlib
              zstd
            ];
          };

          cargoArtifacts = craneLib.buildDepsOnly (commonArgs // { pname = cargoToml.package.name; });
        in
        {
          packages.default = config.packages.bcachefs-tools;
          packages.bcachefs-tools = craneLib.buildPackage (
            commonArgs
            // {
              inherit cargoArtifacts;

              enableParallelBuilding = true;
              buildPhaseCargoCommand = ''
                make ''${enableParallelBuilding:+-j''${NIX_BUILD_CORES}} $makeFlags
              '';
              installPhaseCommand = ''
                make ''${enableParallelBuilding:+-j''${NIX_BUILD_CORES}} $makeFlags install
              '';

              doInstallCheck = true;
              installCheckPhase = ''
                runHook preInstallCheck

                test "$($out/bin/bcachefs version)" = "${commit}"

                runHook postInstallCheck
              '';
            }
          );

          packages.bcachefs-tools-fuse = config.packages.bcachefs-tools.overrideAttrs (
            final: prev: {
              makeFlags = prev.makeFlags ++ [ "BCACHEFS_FUSE=1" ];
              buildInputs = prev.buildInputs ++ [ pkgs.fuse3 ];
            }
          );

          checks.cargo-clippy = craneLib.cargoClippy (
            commonArgs
            // {
              inherit cargoArtifacts;
              cargoClippyExtraArgs = "--all-targets -- --deny warnings";
            }
          );

          # we have to build our own `craneLib.cargoTest`
          checks.cargo-test = craneLib.mkCargoDerivation (
            commonArgs
            // {
              inherit cargoArtifacts;
              doCheck = true;

              enableParallelChecking = true;

              pnameSuffix = "-test";
              buildPhaseCargoCommand = "";
              checkPhaseCargoCommand = ''
                make ''${enableParallelChecking:+-j''${NIX_BUILD_CORES}} $makeFlags libbcachefs.a
                cargo test --profile release -- --nocapture
              '';
            }
          );

          devShells.default = pkgs.mkShell {
            inputsFrom = [
              config.packages.default
              config.treefmt.build.devShell
            ];

            # here go packages that aren't required for builds but are used for
            # development, and might need to be version matched with build
            # dependencies (e.g. clippy or rust-analyzer).
            packages = with pkgs; [
              cargo-audit
              cargo-outdated
              clang-tools
              clippy
              rust-analyzer
              rustc
            ];
          };

          treefmt.config = {
            projectRootFile = "flake.nix";

            programs = {
              nixfmt-rfc-style.enable = true;
              rustfmt.edition = rustfmtToml.edition;
              rustfmt.enable = true;
              rustfmt.package = fenix.packages.${system}.default.rustfmt;
            };
          };
        };
    };
}