|
1 |
| -use std::process::Command; |
| 1 | +use std::process::{Command, Stdio}; |
2 | 2 |
|
3 | 3 | use clap::Parser;
|
4 | 4 | use indoc::indoc;
|
@@ -26,22 +26,49 @@ fn main() {
|
26 | 26 | cmd.arg(args.shell);
|
27 | 27 | }
|
28 | 28 |
|
| 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 | + |
29 | 59 | let expression = format!(
|
30 | 60 | 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}\"; |
34 | 66 | }}"},
|
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, |
45 | 72 | );
|
46 | 73 |
|
47 | 74 | if args.verbose {
|
@@ -100,6 +127,14 @@ struct Opt {
|
100 | 127 | /// Output debug information
|
101 | 128 | #[clap(long)]
|
102 | 129 | 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, |
103 | 138 | }
|
104 | 139 |
|
105 | 140 | #[cfg(test)]
|
|
0 commit comments