Skip to content

Commit

Permalink
Fix building in Nix flake (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
tesujimath authored Oct 18, 2024
1 parent c11d3e0 commit e1e410a
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nu_plugin_bash_env"
version = "0.16.0"
version = "0.16.1"
edition = "2021"
license = "MIT"
description = "Nu plugin bash-env"
Expand All @@ -21,3 +21,6 @@ subprocess = "0.2.9"
tempfile = "3.12.0"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }

[build-dependencies]
anyhow = "1.0.86"
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ The following versions are compatible.
| 0.96 | 0.12.1 |
| 0.97 | 0.13.0 |
| 0.98 | 0.14.2 |
| 0.98 | 0.15.0 |
| 0.99 | 0.16.0 |
| 0.98 | 0.15.1 |
| 0.99 | 0.16.1 |

If you find a new version of Nushell rejects this plugin as incompatible, please report an [issue](https://github.com/tesujimath/nu_plugin_bash_env/issues).

Expand Down
84 changes: 74 additions & 10 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,74 @@
use anyhow::anyhow;
use std::{
env,
fs::{copy, create_dir},
path::{Path, PathBuf},
};

const BASH_ENV_JSON_VERSION: &str = "0.6.1";
// If fetching bash-env-json directly from GitHub, this defines what we fetch.
// Note that when building the package from the Nix Flake, this is not used.
// In that case, the package version is as defined in the Flake input.
const BASH_ENV_JSON_GITHUB_TAG: &str = "0.6.1";

fn fetch_bash_env_json() -> Option<PathBuf> {
let out_dir: PathBuf = env::var("OUT_DIR").unwrap().into();
// install bash-env-json locally if available
fn install_nix_bash_env_json<P>(out_dir: P) -> Option<PathBuf>
where
P: AsRef<Path>,
{
match env::var("NIX_BASH_ENV_JSON") {
Err(_) => {
println!("cargo:warning=install_nix_bash_env_json failed to find NIX_BASH_ENV_JSON, falling back to fetch from GitHub");
None
}
Ok(bash_env_json) => {
let src_path: PathBuf = bash_env_json.into();

let bash_env_json_repo_dir = PathBuf::from("bash-env-json");
let bash_env_json_repo_path = out_dir.as_ref().join(bash_env_json_repo_dir.as_path());

match create_dir(&bash_env_json_repo_path) {
Err(e) => {
println!(
"cargo:warning=install_nix_bash_env_json failed to create directory {}: {}",
bash_env_json_repo_path.to_string_lossy(),
e
);
None
}
Ok(_) => {
let dst_path = bash_env_json_repo_path.join("bash-env-json");
match copy(&src_path, &dst_path) {
Err(e) => {
println!(
"cargo:warning=install_nix_bash_env_json failed to copy {} to {}: {}",
src_path.to_string_lossy(),
&dst_path.to_string_lossy(),
e
);
None
}
Ok(_) => {
println!(
"cargo:warning=install_nix_bash_env_json installed {} as {}",
src_path.to_string_lossy(),
&dst_path.to_string_lossy(),
);
Some(dst_path)
}
}
}
}
}
}
}

// fetch bash-env-json from GitHub
fn fetch_bash_env_json<P>(out_dir: P) -> Option<PathBuf>
where
P: AsRef<Path>,
{
let bash_env_json_repo_dir = PathBuf::from("bash-env-json");
let bash_env_json_repo_path = out_dir.join(bash_env_json_repo_dir.as_path());
let bash_env_json_repo_path = out_dir.as_ref().join(bash_env_json_repo_dir.as_path());

if Path::exists(&bash_env_json_repo_path) {
Some(bash_env_json_repo_path)
Expand All @@ -18,7 +78,7 @@ fn fetch_bash_env_json() -> Option<PathBuf> {
"clone",
"--filter=blob:none",
"--branch",
BASH_ENV_JSON_VERSION,
BASH_ENV_JSON_GITHUB_TAG,
"https://github.com/tesujimath/bash-env-json.git",
bash_env_json_repo_path_str.as_ref(),
];
Expand All @@ -45,10 +105,14 @@ fn fetch_bash_env_json() -> Option<PathBuf> {
}
}

fn main() -> std::io::Result<()> {
if let Some(_bash_env_json_repo_path) = fetch_bash_env_json() {
// TODO what now?
}
fn main() -> anyhow::Result<()> {
let out_dir: PathBuf = env::var("OUT_DIR").unwrap().into();

Ok(())
match install_nix_bash_env_json(&out_dir) {
Some(_path) => Ok(()),
None => match fetch_bash_env_json(&out_dir) {
Some(_path) => Ok(()),
None => Err(anyhow!("failed to fetch bash-env-json from GitHub")),
},
}
}
8 changes: 4 additions & 4 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 36 additions & 18 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
flake-utils.url = "github:numtide/flake-utils";

bash-env-json = {
url = "github:tesujimath/bash-env-json/main";
url = "github:tesujimath/bash-env-json?ref=refs/tags/0.6.1";
inputs.nixpkgs.follows = "nixpkgs";
};
};
Expand All @@ -24,21 +24,24 @@
inherit system overlays;
};

inherit (pkgs) rust-bin writeShellScriptBin;
flakePkgs = {
bash-env-json = bash-env-json.packages.${system}.default;
};

# cargo-nightly based on https://github.com/oxalica/rust-overlay/issues/82
nightly = pkgs.rust-bin.selectLatestNightlyWith (t: t.default);
cargo-nightly = pkgs.writeShellScriptBin "cargo-nightly" ''
nightly = rust-bin.selectLatestNightlyWith (t: t.default);
cargo-nightly = writeShellScriptBin "cargo-nightly" ''
export RUSTC="${nightly}/bin/rustc";
exec "${nightly}/bin/cargo" "$@"
'';

nu_plugin_bash_env =
let cargoConfig = builtins.fromTOML (builtins.readFile ./Cargo.toml);
let
inherit (pkgs) lib makeWrapper rustPlatform;
cargoConfig = builtins.fromTOML (builtins.readFile ./Cargo.toml);
in
pkgs.rustPlatform.buildRustPackage
rustPlatform.buildRustPackage
{
pname = "nu_plugin_bash_env";
version = cargoConfig.package.version;
Expand All @@ -49,35 +52,50 @@
lockFile = ./Cargo.lock;
};

meta = with pkgs.lib; {
meta = with lib; {
description = "A Bash environment plugin for Nushell";
homepage = "https://github.com/tesujimath/nu_plugin_bash_env";
license = licenses.mit;
maintainers = [ maintainers.tailhook ];
};

buildInputs = [
pkgs.makeWrapper
makeWrapper
flakePkgs.bash-env-json
];


preBuild = ''
export NIX_BASH_ENV_JSON=${flakePkgs.bash-env-json}/bin/bash-env-json
'';

postFixup = ''
wrapProgram $out/bin/nu_plugin_bash_env --set NU_PLUGIN_BASH_ENV_JSON ${flakePkgs.bash-env-json}/bin/bash-env-json
'';
};
in
with pkgs;
{
devShells.default = mkShell {
nativeBuildInputs = [
bashInteractive
jq
cargo-modules
cargo-nightly
cargo-udeps
rust-bin.stable.latest.default
];
};
devShells.default =
let
inherit (pkgs)
mkShell
bashInteractive
jq
cargo-modules
cargo-udeps
rust-bin;
in

mkShell {
nativeBuildInputs = [
bashInteractive
jq
cargo-modules
cargo-nightly
cargo-udeps
rust-bin.stable.latest.default
];
};

packages.default = nu_plugin_bash_env;
}
Expand Down

0 comments on commit e1e410a

Please sign in to comment.