Skip to content

Commit

Permalink
Add configurations to disable expensive evaluations
Browse files Browse the repository at this point in the history
  • Loading branch information
oxalica committed May 6, 2023
1 parent 0b36ff7 commit 21d0870
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
28 changes: 28 additions & 0 deletions crates/nil/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub struct Config {
pub formatting_command: Option<Vec<String>>,
pub nix_binary: PathBuf,
pub nix_flake_auto_archive: Option<bool>,
pub nix_flake_auto_eval_inputs: bool,
pub nix_flake_nixpkgs_input_name: Option<String>,
}

impl Config {
Expand All @@ -25,6 +27,8 @@ impl Config {
formatting_command: None,
nix_binary: "nix".into(),
nix_flake_auto_archive: None,
nix_flake_auto_eval_inputs: true,
nix_flake_nixpkgs_input_name: Some("nixpkgs".into()),
}
}

Expand Down Expand Up @@ -97,6 +101,30 @@ impl Config {
}
}

if let Some(v) = value.pointer_mut("/nix/flake/autoEvalInputs") {
match serde_json::from_value::<bool>(v.take()) {
Ok(value) => {
self.nix_flake_auto_eval_inputs = value;
}
Err(e) => {
errors.push(format!("Invalid value of `nix.flake.autoArchive`: {e}"));
}
}
}

if let Some(v) = value.pointer_mut("/nix/flake/nixpkgsInputName") {
match serde_json::from_value::<Option<String>>(v.take()) {
Ok(value) => {
self.nix_flake_nixpkgs_input_name = value;
}
Err(e) => {
errors.push(format!(
"Invalid value of `nix.flake.nixpkgsInputName`: {e}"
));
}
}
}

(errors, updated_diagnostics)
}
}
24 changes: 13 additions & 11 deletions crates/nil/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ const FLAKE_ARCHIVE_PROGRESS_TOKEN: &str = "nil/flakeArchiveProgress";
const LOAD_INPUT_FLAKE_PROGRESS_TOKEN: &str = "nil/loadInputFlakeProgress";
const LOAD_NIXOS_OPTIONS_PROGRESS_TOKEN: &str = "nil/loadNixosOptionsProgress";

const NIXOS_OPTIONS_FLAKE_INPUT: &str = "nixpkgs";

const PROGRESS_REPORT_PERIOD: Duration = Duration::from_millis(100);
const LOAD_FLAKE_WORKSPACE_DEBOUNCE_DURATION: Duration = Duration::from_millis(100);

Expand Down Expand Up @@ -553,20 +551,22 @@ impl Server {
}
}

// TODO: A better way to retrieve the nixpkgs for options?
if let Some(nixpkgs_path) = flake_info
.input_store_paths
.get(NIXOS_OPTIONS_FLAKE_INPUT)
.and_then(VfsPath::as_path)
.filter(|path| path.exists())
{
if let Some((input_name, nixpkgs_path)) = (|| {
let input_name = config.nix_flake_nixpkgs_input_name.as_ref()?;
let path = flake_info
.input_store_paths
.get(input_name)?
.as_path()
.filter(|p| p.exists())?;
Some((input_name, path))
})() {
tracing::info!("Evaluating NixOS options from {}", nixpkgs_path.display());

let _progress = Progress::new(
&client,
&caps,
LOAD_NIXOS_OPTIONS_PROGRESS_TOKEN,
"Loading NixOS options",
format!("Loading NixOS options from '{input_name}'"),
None,
)
.await;
Expand All @@ -587,7 +587,9 @@ impl Server {
}
}

Self::load_input_flakes(flake_info, &config, &caps, &mut client).await;
if config.nix_flake_auto_eval_inputs {
Self::load_input_flakes(flake_info, &config, &caps, &mut client).await;
}
}

async fn load_input_flakes(
Expand Down
19 changes: 18 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,25 @@ Default configuration:
// - true: Automaticaly run `nix flake archive` when necessary.
// - false: Do not archive. Only load inputs that are already on disk.
// Type: null | boolean
// Example: true,
// Example: true
"autoArchive": null,
// Whether to auto-eval flake inputs.
// The evaluation result is used to improve completion, but may cost
// lots of time and/or memory.
//
// Type: boolean
// Example: false
"autoEvalInputs": true,
// The input name of nixpkgs for NixOS options evaluation.
//
// The options hierarchy is used to improve completion, but may cost
// lots of time and/or memory.
// If this value is `null` or is not found in the workspace flake's
// inputs, NixOS options are not evaluated.
//
// Type: null | string
// Example: "nixos"
"nixpkgsInputName": "nixpkgs",
},
},
},
Expand Down

0 comments on commit 21d0870

Please sign in to comment.