Skip to content

Commit 48aadeb

Browse files
Use fenix from NIX_PATH if it is present
1 parent 4aaee6f commit 48aadeb

File tree

2 files changed

+54
-16
lines changed

2 files changed

+54
-16
lines changed

src/main.rs

+49-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::process::Command;
1+
use std::process::{Command, Stdio};
22

33
use clap::Parser;
44
use indoc::indoc;
@@ -26,22 +26,49 @@ fn main() {
2626
cmd.arg(args.shell);
2727
}
2828

29+
let toolchain_attrs = match args.channel {
30+
RustChannel::Stable => r#"{channel = "stable";}"#.to_owned(),
31+
RustChannel::Beta => r#"{channel = "beta";}"#.to_owned(),
32+
RustChannel::Nightly => r#"{channel = "nightly";}"#.to_owned(),
33+
RustChannel::DatedNightly(date) => format!(r#"{{channel = "nightly"; date = "{date}";}}"#),
34+
RustChannel::Version(version) => format!(r#"{{channel = "{version}";}}"#),
35+
};
36+
37+
let fenix_expr = {
38+
let fenix_in_nix_path = {
39+
let mut cmd = Command::new("nix");
40+
cmd.args([
41+
"--extra-experimental-features",
42+
"nix-command flakes",
43+
"eval",
44+
"--impure",
45+
"--expr",
46+
"<fenix>",
47+
]);
48+
cmd.stdout(Stdio::null());
49+
let ret = cmd.status().expect("Nix command should not fail");
50+
ret.code().unwrap() == 0
51+
};
52+
53+
match fenix_in_nix_path {
54+
true if !args.fresh_fenix => "<fenix>",
55+
_ => r#"fetchTarball "https://github.com/nix-community/fenix/archive/main.tar.gz""#,
56+
}
57+
};
58+
2959
let expression = format!(
3060
indoc! {"
31-
({}) {{
32-
toolchainAttrs = {};
33-
otherDeps = with import <nixpkgs> {{}}; [{}];
61+
({rust_nix}) {{
62+
toolchainAttrs = {toolchain_attrs};
63+
otherDeps = with import <nixpkgs> {{}}; [{extra_packages}];
64+
fenix = import ({fenix_expr}) {{}};
65+
name = \"{drv_name}\";
3466
}}"},
35-
include_str!("rust.nix"),
36-
match args.channel {
37-
RustChannel::Stable => r#"{channel = "stable";}"#.to_owned(),
38-
RustChannel::Beta => r#"{channel = "beta";}"#.to_owned(),
39-
RustChannel::Nightly => r#"{channel = "nightly";}"#.to_owned(),
40-
RustChannel::DatedNightly(date) =>
41-
format!(r#"{{channel = "nightly"; date = "{date}";}}"#),
42-
RustChannel::Version(version) => format!(r#"{{channel = "{version}";}}"#),
43-
},
44-
args.packages.join(" ")
67+
rust_nix = include_str!("rust.nix"),
68+
toolchain_attrs = toolchain_attrs,
69+
extra_packages = args.packages.join(" "),
70+
drv_name = args.name,
71+
fenix_expr = fenix_expr,
4572
);
4673

4774
if args.verbose {
@@ -100,6 +127,14 @@ struct Opt {
100127
/// Output debug information
101128
#[clap(long)]
102129
verbose: bool,
130+
131+
/// The name for the shell
132+
#[clap(long, default_value = "rust-nix-shell")]
133+
name: String,
134+
135+
/// Force the use of a fresh fenix
136+
#[clap(long)]
137+
fresh_fenix: bool,
103138
}
104139

105140
#[cfg(test)]

src/rust.nix

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
toolchainAttrs,
33
otherDeps,
4+
fenix,
5+
name,
46
}: let
57
rust-nix-shell = {
68
mkShell,
@@ -10,7 +12,7 @@
1012
rustPlatform,
1113
}:
1214
mkShell {
13-
name = "rust-nix-shell";
15+
inherit name;
1416
buildInputs =
1517
[
1618
(toolchain.withComponents ["cargo" "rustc" "rust-src" "rustfmt" "clippy"])
@@ -24,7 +26,8 @@
2426
RUST_SRC_PATH = "${toolchain.rust-src}";
2527
};
2628
pkgs = import <nixpkgs> {};
27-
fenix = import (fetchTarball "https://github.com/nix-community/fenix/archive/main.tar.gz") {};
29+
inherit fenix;
30+
# fenix = import (fetchTarball "https://github.com/nix-community/fenix/archive/main.tar.gz") {};
2831
toolchain = fenix.toolchainOf toolchainAttrs;
2932
in
3033
pkgs.callPackage rust-nix-shell {

0 commit comments

Comments
 (0)