summaryrefslogtreecommitdiff
path: root/flake.nix
blob: eb3a31cb17c0dd97a35ddbe4656ee35a8fe55fee (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
{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs?ref=nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";

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

  outputs =
    { self
    , nixpkgs
    , flake-utils

    , fenix
    , crane
    }: flake-utils.lib.eachDefaultSystem (system:
    let
      pkgs = nixpkgs.legacyPackages.${system};

      # Use mold on Linux
      stdenv = if pkgs.stdenv.isLinux then
        pkgs.stdenvAdapters.useMoldLinker pkgs.stdenv
      else
        pkgs.stdenv;

      # Nix-accessible `Cargo.toml`
      cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);

      # The Rust toolchain to use
      toolchain = fenix.packages.${system}.toolchainOf {
        # Use the Rust version defined in `Cargo.toml`
        channel = cargoToml.package.rust-version;

        # THE rust-version HASH
        sha256 = "sha256-gdYqng0y9iHYzYPAdkC/ka3DRny3La/S5G8ASj0Ayyc=";
      };

      # The system's RocksDB
      ROCKSDB_INCLUDE_DIR = "${pkgs.rocksdb}/include";
      ROCKSDB_LIB_DIR = "${pkgs.rocksdb}/lib";

      # Shared between the package and the devShell
      nativeBuildInputs = (with pkgs.rustPlatform; [
        bindgenHook
      ]);

      builder =
        ((crane.mkLib pkgs).overrideToolchain toolchain.toolchain).buildPackage;
    in
    {
      packages.default = builder {
        src = ./.;

        inherit
          stdenv
          nativeBuildInputs
          ROCKSDB_INCLUDE_DIR
          ROCKSDB_LIB_DIR;
      };

      devShells.default = (pkgs.mkShell.override { inherit stdenv; }) {
        # Rust Analyzer needs to be able to find the path to default crate
        # sources, and it can read this environment variable to do so
        RUST_SRC_PATH = "${toolchain.rust-src}/lib/rustlib/src/rust/library";

        inherit
          ROCKSDB_INCLUDE_DIR
          ROCKSDB_LIB_DIR;

        # Development tools
        nativeBuildInputs = nativeBuildInputs ++ (with toolchain; [
          cargo
          clippy
          rust-src
          rustc
          rustfmt
        ]);
      };

      checks = {
        packagesDefault = self.packages.${system}.default;
        devShellsDefault = self.devShells.${system}.default;
      };
    });
}