Skip to content

Commit 76caf3b

Browse files
committed
Add nix flake
1 parent 31dda8a commit 76caf3b

3 files changed

Lines changed: 126 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/target
2+
result

flake.lock

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
description = "A modern Rust project environment using Nix and Crane";
3+
4+
inputs = {
5+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
6+
crane.url = "github:ipetkov/crane";
7+
systems.url = "github:nix-systems/default";
8+
};
9+
10+
outputs = { self, nixpkgs, crane, systems, ... }:
11+
let
12+
forEachSystem = f: nixpkgs.lib.genAttrs (import systems) (system: f rec {
13+
pkgs = import nixpkgs { inherit system; };
14+
craneLib = crane.mkLib pkgs;
15+
src = let
16+
fixturesFilter = path: _type: builtins.match ".*/examples/.*" path != null;
17+
fixturesOrCargo = path: type: (fixturesFilter path type) || (craneLib.filterCargoSources path type);
18+
in pkgs.lib.cleanSourceWith {
19+
src = craneLib.path ./.;
20+
filter = fixturesOrCargo;
21+
};
22+
});
23+
in
24+
{
25+
packages = forEachSystem ({ pkgs, craneLib, src }:
26+
let
27+
commonArgs = {
28+
inherit src;
29+
strictDeps = true;
30+
nativeBuildInputs = with pkgs; [ pkg-config ];
31+
buildInputs = with pkgs; [
32+
openssl
33+
] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
34+
pkgs.darwin.apple_sdk.frameworks.Security
35+
pkgs.darwin.apple_sdk.frameworks.SystemConfiguration
36+
];
37+
};
38+
39+
# Build *only* the dependencies to cache them in the Nix store
40+
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
41+
in
42+
{
43+
default = craneLib.buildPackage (commonArgs // {
44+
inherit cargoArtifacts;
45+
});
46+
}
47+
);
48+
49+
devShells = forEachSystem ({ pkgs, craneLib, src }: {
50+
default = craneLib.devShell {
51+
inputsFrom = [ self.packages.${pkgs.system}.default ];
52+
packages = with pkgs; [
53+
cargo
54+
rustc
55+
rust-analyzer
56+
rustfmt
57+
clippy
58+
];
59+
env = {
60+
RUST_SRC_PATH = "${pkgs.rustPlatform.rustLibSrc}";
61+
RUST_BACKTRACE = "1";
62+
};
63+
};
64+
});
65+
};
66+
}

0 commit comments

Comments
 (0)