forked from olebedev/cached-nix-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
66 lines (61 loc) · 2.3 KB
/
build.rs
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
use std::env::{var, var_os};
use std::path::Path;
use std::process::Command;
fn main() {
if var_os("CNS_IN_NIX_SHELL").is_none() {
// Release build triggered by nix-build. Use paths relative to $out.
let out = var("out").unwrap();
println!("cargo:rustc-env=CNS_TRACE_NIX_SO={out}/lib/trace-nix.so");
println!("cargo:rustc-env=CNS_VAR_EMPTY={out}/var/empty");
println!(
"cargo:rustc-env=CNS_RCFILE={out}/share/cached-nix-shell/rcfile.sh"
);
println!(
"cargo:rustc-env=CNS_WRAP_PATH={out}/libexec/cached-nix-shell"
);
// Use pinned nix and nix-shell binaries.
println!(
"cargo:rustc-env=CNS_NIX={}/",
which::which("nix")
.expect("command not found: nix")
.parent()
.unwrap()
.as_os_str()
.to_str()
.unwrap()
);
} else {
// Developer build triggered by `nix-shell --run 'cargo build'`.
// Use paths relative to the build directory. Additionally, place
// trace-nix.so and a symlink to the build directory.
let out_dir = var("OUT_DIR").unwrap();
let cmd = Command::new("make")
.args([
"-C",
"nix-trace",
&format!("DESTDIR={out_dir}"),
&format!("{out_dir}/trace-nix.so"),
])
.status()
.unwrap();
assert!(cmd.success());
println!("cargo:rustc-env=CNS_TRACE_NIX_SO={out_dir}/trace-nix.so");
println!("cargo:rustc-env=CNS_VAR_EMPTY=/var/empty");
println!(
"cargo:rustc-env=CNS_RCFILE={}/rcfile.sh",
var("CARGO_MANIFEST_DIR").unwrap()
);
if Path::new(&format!("{out_dir}/wrapper")).exists() {
std::fs::remove_dir_all(format!("{out_dir}/wrapper")).unwrap();
}
std::fs::create_dir_all(format!("{out_dir}/wrapper")).unwrap();
std::os::unix::fs::symlink(
"../../../../cached-nix-shell",
format!("{out_dir}/wrapper/nix-shell"),
)
.unwrap();
println!("cargo:rustc-env=CNS_WRAP_PATH={out_dir}/wrapper");
// Use nix and nix-shell from $PATH at runtime.
println!("cargo:rustc-env=CNS_NIX=");
}
}