Skip to content

Commit

Permalink
add support for nix store installables
Browse files Browse the repository at this point in the history
  • Loading branch information
viperML committed Nov 18, 2024
1 parent 3e6c55a commit 4198d6a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/installable.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::env;
use std::path::PathBuf;
use std::{env, fs};

use clap::error::ErrorKind;
use clap::{Arg, ArgAction, Args, FromArgMatches};
Expand All @@ -17,10 +17,9 @@ pub enum Installable {
path: PathBuf,
attribute: Vec<String>,
},
// TODO:
// Store {
// path: PathBuf,
// },
Store {
path: PathBuf,
},
Expression {
expression: String,
attribute: Vec<String>,
Expand All @@ -38,6 +37,16 @@ impl FromArgMatches for Installable {
let file = matches.get_one::<String>("file");
let expr = matches.get_one::<String>("expr");

if let Some(i) = installable {
let canonincal = fs::canonicalize(i);

if let Ok(p) = canonincal {
if p.starts_with("/nix/store") {
return Ok(Self::Store { path: p });
}
}
}

if let Some(f) = file {
return Ok(Self::File {
path: PathBuf::from(f),
Expand Down Expand Up @@ -123,6 +132,9 @@ Nix accepts various kinds of installables:
{}, {} <EXPR> [ATTRPATH]
Nix expression with an optional attribute path.
[PATH]
Path or symlink to a /nix/store path
"#,
env::var("NH_FLAKE").unwrap_or_default(),
"-f".yellow(),
Expand Down Expand Up @@ -181,6 +193,7 @@ impl Installable {
res.push(expression.to_string());
res.push(join_attribute(attribute));
}
Installable::Store { path } => res.push(path.to_str().unwrap().to_string()),
}

res
Expand Down
5 changes: 5 additions & 0 deletions src/nixos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ fn toplevel_for<S: AsRef<str>>(hostname: S, installable: Installable) -> Install
} => {
attribute.extend(toplevel);
}
Installable::Store { .. } => {}
}

res
Expand All @@ -194,6 +195,10 @@ impl OsReplArgs {
fn run(self) -> Result<()> {
let mut target_installable = self.installable;

if matches!(target_installable, Installable::Store { .. }) {
bail!("Nix doesn't support nix store installables.");
}

let hostname = self
.hostname
.unwrap_or_else(|| hostname::get().unwrap().to_str().unwrap().to_string());
Expand Down

0 comments on commit 4198d6a

Please sign in to comment.