Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically detect Nixpkgs inputs #130

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "flake-checker"
version = "0.2.0"
version = "0.2.1"
edition = "2021"

[workspace]
Expand Down
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
cargo-bloat
cargo-edit
cargo-machete
cargo-watch
bacon
rust-analyzer

# Nix
Expand Down
6 changes: 3 additions & 3 deletions src/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use parse_flake_lock::{FlakeLock, Node};

use crate::{
error::FlakeCheckerError,
flake::{nixpkgs_deps, num_days_old},
flake::{nixpkgs_deps, num_days_old, FlakeCheckConfig},
issue::{Issue, IssueKind},
};

Expand All @@ -14,15 +14,15 @@ const KEY_SUPPORTED_REFS: &str = "supportedRefs";

pub(super) fn evaluate_condition(
flake_lock: &FlakeLock,
nixpkgs_keys: &[String],
config: &FlakeCheckConfig,
condition: &str,
supported_refs: Vec<String>,
) -> Result<Vec<Issue>, FlakeCheckerError> {
let mut issues: Vec<Issue> = vec![];
let mut ctx = Context::default();
ctx.add_variable_from_value(KEY_SUPPORTED_REFS, supported_refs);

let deps = nixpkgs_deps(flake_lock, nixpkgs_keys)?;
let deps = nixpkgs_deps(flake_lock, config.nixpkgs_keys.clone())?;

for (name, node) in deps {
let (git_ref, last_modified, owner) = match node {
Expand Down
106 changes: 64 additions & 42 deletions src/flake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub(crate) struct FlakeCheckConfig {
pub check_outdated: bool,
pub check_owner: bool,
pub fail_mode: bool,
pub nixpkgs_keys: Vec<String>,
pub nixpkgs_keys: Option<Vec<String>>,
}

impl Default for FlakeCheckConfig {
Expand All @@ -25,52 +25,79 @@ impl Default for FlakeCheckConfig {
check_outdated: true,
check_owner: true,
fail_mode: false,
nixpkgs_keys: vec![String::from("nixpkgs")],
nixpkgs_keys: None,
}
}
}

pub(super) fn nixpkgs_deps(
flake_lock: &FlakeLock,
keys: &[String],
nixpkgs_keys: Option<Vec<String>>,
) -> Result<HashMap<String, Node>, FlakeCheckerError> {
let mut deps: HashMap<String, Node> = HashMap::new();

for (ref key, node) in flake_lock.root.clone() {
match &node {
Node::Repo(_) => {
if keys.contains(key) {
deps.insert(key.to_string(), node);
if let Some(explicit_keys) = nixpkgs_keys {
for (ref key, node) in flake_lock.root.clone() {
match &node {
Node::Repo(_) => {
if explicit_keys.contains(key) {
deps.insert(key.to_string(), node);
}
}
}
Node::Tarball(_) => {
if keys.contains(key) {
deps.insert(key.to_string(), node);
Node::Tarball(_) => {
if explicit_keys.contains(key) {
deps.insert(key.to_string(), node);
}
}
}
Node::Indirect(indirect_node) => {
if keys.contains(key) && &indirect_node.original.id == key {
deps.insert(key.to_string(), node);
Node::Indirect(indirect_node) => {
if explicit_keys.contains(key) && &indirect_node.original.id == key {
deps.insert(key.to_string(), node);
}
}
_ => {
// NOTE: it's unclear that a path node for Nixpkgs should be accepted
}
}
_ => {
// NOTE: it's unclear that a path node for Nixpkgs should be accepted

let missing: Vec<String> = explicit_keys
.iter()
.filter(|k| !deps.contains_key(*k))
.map(String::from)
.collect();

if !missing.is_empty() {
let error_msg = format!(
"no nixpkgs dependency found for specified {}: {}",
if missing.len() > 1 { "keys" } else { "key" },
missing.join(", ")
);
return Err(FlakeCheckerError::Invalid(error_msg));
}
}
} else {
for (ref key, node) in flake_lock.root.clone() {
match &node {
Node::Repo(repo) => {
if repo.original.owner.to_lowercase() == "nixos"
&& repo.original.repo.to_lowercase() == "nixpkgs"
{
deps.insert(key.to_string(), node);
}
}
Node::Tarball(tarball) => {
// If necessary, we can expand this to include other tarball sources
if tarball
.original
.url
.to_lowercase()
.starts_with("https://flakehub.com/f/nixos/nixpkgs/")
{
deps.insert(key.to_string(), node);
}
}
_ => {}
}
}
}
let missing: Vec<String> = keys
.iter()
.filter(|k| !deps.contains_key(*k))
.map(String::from)
.collect();

if !missing.is_empty() {
let error_msg = format!(
"no nixpkgs dependency found for specified {}: {}",
if missing.len() > 1 { "keys" } else { "key" },
missing.join(", ")
);
return Err(FlakeCheckerError::Invalid(error_msg));
}

Ok(deps)
Expand All @@ -83,7 +110,7 @@ pub(crate) fn check_flake_lock(
) -> Result<Vec<Issue>, FlakeCheckerError> {
let mut issues = vec![];

let deps = nixpkgs_deps(flake_lock, &config.nixpkgs_keys)?;
let deps = nixpkgs_deps(flake_lock, config.nixpkgs_keys.clone())?;

for (name, node) in deps {
let (git_ref, last_modified, owner) = match node {
Expand Down Expand Up @@ -177,16 +204,11 @@ mod test {
for (condition, expected) in cases {
let flake_lock = FlakeLock::new(&path).unwrap();
let config = FlakeCheckConfig {
nixpkgs_keys: vec![String::from("nixpkgs")],
..Default::default()
};

let result = evaluate_condition(
&flake_lock,
&config.nixpkgs_keys,
condition,
supported_refs.clone(),
);
let result =
evaluate_condition(&flake_lock, &config, condition, supported_refs.clone());

if expected {
assert!(result.is_ok());
Expand Down Expand Up @@ -291,7 +313,7 @@ mod test {
let flake_lock = FlakeLock::new(&path).unwrap();
let config = FlakeCheckConfig {
check_outdated: false,
nixpkgs_keys,
nixpkgs_keys: Some(nixpkgs_keys),
..Default::default()
};
let issues = check_flake_lock(&flake_lock, &config, allowed_refs.clone()).unwrap();
Expand All @@ -318,7 +340,7 @@ mod test {
let flake_lock = FlakeLock::new(&path).unwrap();
let config = FlakeCheckConfig {
check_outdated: false,
nixpkgs_keys,
nixpkgs_keys: Some(nixpkgs_keys),
..Default::default()
};

Expand Down
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,10 @@ struct Cli {
long,
short,
env = "NIX_FLAKE_CHECKER_NIXPKGS_KEYS",
default_value = "nixpkgs",
value_delimiter = ',',
name = "KEY_LIST"
)]
nixpkgs_keys: Vec<String>,
nixpkgs_keys: Option<Vec<String>>,

/// Display Markdown summary (in GitHub Actions).
#[arg(
Expand Down Expand Up @@ -179,7 +178,12 @@ fn main() -> Result<ExitCode, FlakeCheckerError> {
};

let issues = if let Some(condition) = &condition {
evaluate_condition(&flake_lock, &nixpkgs_keys, condition, allowed_refs.clone())?
evaluate_condition(
&flake_lock,
&flake_check_config,
condition,
allowed_refs.clone(),
)?
} else {
check_flake_lock(&flake_lock, &flake_check_config, allowed_refs.clone())?
};
Expand Down
Loading