Skip to content

Commit

Permalink
--cores defaults to 2, --max-jobs defaults to num_cpu/2
Browse files Browse the repository at this point in the history
  • Loading branch information
domenkozar committed Mar 13, 2024
1 parent bbbe0a3 commit 02c48f6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
16 changes: 8 additions & 8 deletions devenv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,17 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1710222005,
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "9a9a7552431c4f1a3b2eee9398641babf7c30d0e",
"treeHash": "f86a6354c1b9536fd6eec80186c371c5b09c0be8",
"lastModified": 1710236354,
"owner": "cachix",
"repo": "devenv-nixpkgs",
"rev": "829e73affeadfb4198a7105cbe3a03153d13edc9",
"treeHash": "c92d1301b904a3d47027cca315d4de397f711280",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"owner": "cachix",
"ref": "rolling",
"repo": "devenv-nixpkgs",
"type": "github"
}
},
Expand Down
3 changes: 3 additions & 0 deletions devenv/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ impl App {
) -> Result<std::process::Command> {
let cmd = if command.starts_with("nix") {
let mut flags = NIX_FLAGS.to_vec();
flags.push("--max-jobs");
let max_jobs = self.cli.max_jobs.to_string();
flags.push(&max_jobs);

// handle --nix-option key value
for chunk in self.cli.nix_option.chunks_exact(2) {
Expand Down
27 changes: 23 additions & 4 deletions devenv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ struct Cli {
#[arg(short, long, help = "Enable debug log level.")]
verbose: bool,

#[arg(short = 'j', long, help = "Maximum number of Nix builds at any time.", default_value_t = max_jobs())]
max_jobs: u8,

#[arg(
short = 'j',
long,
help = "Maximum number CPU cores being used by a single build..",
default_value = "2"
)]
cores: u8,

#[arg(short, long, default_value_t = default_system())]
system: String,

Expand Down Expand Up @@ -376,7 +387,7 @@ impl App {
for filename in REQUIRED_FILES {
let file_path = target.join(filename);
if file_path.exists() {
panic!("File already exists {}", file_path.display());
bail!("File already exists {}", file_path.display());
}
}

Expand Down Expand Up @@ -404,10 +415,10 @@ impl App {
if !status.success() {
match status.code() {
Some(code) => {
panic!("direnv allow failed with code: {code}!");
bail!("direnv allow failed with code: {code}!");
}
None => {
panic!("direnv allow failed!");
bail!("direnv allow failed!");
}
}
}
Expand Down Expand Up @@ -1034,7 +1045,7 @@ impl App {

fn assemble(&mut self) -> Result<()> {
if !PathBuf::from("devenv.nix").exists() {
panic!(indoc::indoc! {"
bail!(indoc::indoc! {"
File devenv.nix does not exist. To get started, run:
$ devenv init
Expand Down Expand Up @@ -1233,3 +1244,11 @@ fn cleanup_symlinks(root: &Path) -> (Vec<PathBuf>, Vec<PathBuf>) {

(to_gc, removed_symlinks)
}

fn max_jobs() -> u8 {
let num_cpus = std::thread::available_parallelism().unwrap_or_else(|e| {
eprintln!("Failed to get number of logical CPUs: {}", e);
std::num::NonZeroUsize::new(1).unwrap()
});
(num_cpus.get() / 2).try_into().unwrap()
}
4 changes: 1 addition & 3 deletions tests/cli/.test.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
set -xe

# TODO: provide devenv cli via inputs
export PATH=$(pwd)/../../result/bin:$PATH

rm devenv.yaml || true
devenv build languages.python.package
devenv shell ls -- -la | grep ".test.sh"
devenv shell ls ../ | grep "cli"
Expand Down

0 comments on commit 02c48f6

Please sign in to comment.