-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflake.nix
107 lines (97 loc) · 3.46 KB
/
flake.nix
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
# From https://github.com/litchipi/nix-build-templates/blob/6e4961dc56a9bbfa3acf316d81861f5bd1ea37ca/rust/maturin.nix
# See also https://discourse.nixos.org/t/pyo3-maturin-python-native-dependency-management-vs-nixpkgs/21739/2
{
# Build Pyo3 package
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-23.05";
flake-utils.url = "github:numtide/flake-utils";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
crane = {
url = "github:ipetkov/crane";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = inputs:
inputs.flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import inputs.nixpkgs {
inherit system;
overlays = [ inputs.rust-overlay.overlays.default ];
};
lib = pkgs.lib;
# Get a custom rust toolchain
customRustToolchain = pkgs.rust-bin.stable."1.70.0".default;
craneLib =
(inputs.crane.mkLib pkgs).overrideToolchain customRustToolchain;
projectName =
(craneLib.crateNameFromCargoToml { cargoToml = ./Cargo.toml; }).pname;
projectVersion = (craneLib.crateNameFromCargoToml {
cargoToml = ./Cargo.toml;
}).version;
pythonVersion = pkgs.python310;
wheelTail =
"cp310-cp310-manylinux_2_34_x86_64"; # Change if pythonVersion changes
wheelName = "${projectName}-${projectVersion}-${wheelTail}.whl";
crateCfg = {
src = craneLib.cleanCargoSource (craneLib.path ./.);
nativeBuildInputs = [ pythonVersion ];
};
# Build the library, then re-use the target dir to generate the wheel file with maturin
crateWheel = (craneLib.buildPackage (crateCfg // {
pname = projectName;
version = projectVersion;
# cargoArtifacts = crateArtifacts;
})).overrideAttrs (old: {
nativeBuildInputs = old.nativeBuildInputs ++ [ pkgs.maturin ];
buildPhase = old.buildPhase + ''
maturin build --offline --target-dir ./target
'';
installPhase = old.installPhase + ''
cp target/wheels/${wheelName} $out/
'';
});
in rec {
packages = rec {
default = crateWheel; # The wheel itself
# A python version with the library installed
pythonEnv = pythonVersion.withPackages
(ps: [ (lib.pythonPackage ps) ] ++ (with ps; [ ipython ]));
};
lib = {
# To use in other builds with the "withPackages" call
pythonPackage = ps:
ps.buildPythonPackage rec {
pname = projectName;
format = "wheel";
version = projectVersion;
src = "${crateWheel}/${wheelName}";
doCheck = false;
pythonImportsCheck = [ projectName ];
};
};
devShells = rec {
rust = pkgs.mkShell {
name = "rust-env";
src = ./.;
nativeBuildInputs = with pkgs; [ pkg-config rust-analyzer maturin ];
};
python = let
in pkgs.mkShell {
name = "python-env";
src = ./.;
nativeBuildInputs = [ packages.pythonEnv ];
};
default = rust;
};
apps = rec {
ipython = {
type = "app";
program = "${packages.pythonEnv}/bin/ipython";
};
default = ipython;
};
});
}