-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from nikstur/ensure-runtime-dependencies
Ensure runtime dependencies
- Loading branch information
Showing
10 changed files
with
139 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
# This is a wrapper around nixpkgs' closureInfo. It returns a newline | ||
# This is a wrapper around nixpkgs' closureInfo. It returns a newline | ||
# separated list of the store paths of drv's runtime dependencies. | ||
{ runCommand | ||
, closureInfo | ||
}: | ||
|
||
drv: | ||
runCommand "${drv.name}-runtime-dependencies.json" { } '' | ||
runCommand "${drv.name}-runtime-dependencies.txt" { } '' | ||
cat ${closureInfo { rootPaths = [ drv ]; }}/store-paths > $out | ||
'' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,24 @@ | ||
use std::hash::{Hash, Hasher}; | ||
use std::collections::HashMap; | ||
use std::fs; | ||
use std::path::Path; | ||
|
||
use serde::Deserialize; | ||
use anyhow::{Context, Result}; | ||
|
||
#[derive(Deserialize, Debug)] | ||
pub struct BuildtimeInput(Vec<Derivation>); | ||
use crate::derivation::Derivation; | ||
|
||
impl BuildtimeInput { | ||
pub fn remove_derivation(&mut self, derivation_path: &str) -> Derivation { | ||
let index = self | ||
.0 | ||
.iter() | ||
.position(|derivation| derivation.path == derivation_path) | ||
.expect("Unrecovereable error: buildtime input does not include target"); | ||
self.0.swap_remove(index) | ||
} | ||
} | ||
|
||
impl IntoIterator for BuildtimeInput { | ||
type Item = Derivation; | ||
type IntoIter = std::vec::IntoIter<Self::Item>; | ||
|
||
fn into_iter(self) -> Self::IntoIter { | ||
self.0.into_iter() | ||
} | ||
} | ||
|
||
#[derive(Deserialize, Clone, Debug)] | ||
pub struct Derivation { | ||
pub path: String, | ||
pub name: Option<String>, | ||
pub pname: Option<String>, | ||
pub version: Option<String>, | ||
pub meta: Option<Meta>, | ||
} | ||
|
||
// Implement Eq and Hash so Itertools::unique can identify unique dependencies by path. The name | ||
// seems to be the best proxy to detect duplicates. Different outputs of the same derivation have | ||
// different paths. Thus, filtering by path alone doesn't adequately remove duplicates. | ||
impl PartialEq for Derivation { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.name == other.name | ||
} | ||
} | ||
impl Eq for Derivation {} | ||
#[derive(Clone)] | ||
pub struct BuildtimeInput(pub HashMap<String, Derivation>); | ||
|
||
impl Hash for Derivation { | ||
fn hash<H: Hasher>(&self, state: &mut H) { | ||
self.name.hash(state); | ||
} | ||
} | ||
|
||
#[derive(Deserialize, Clone, Debug)] | ||
pub struct Meta { | ||
pub license: Option<LicenseField>, | ||
pub homepage: Option<String>, | ||
} | ||
|
||
#[derive(Deserialize, Clone, Debug)] | ||
#[serde(untagged)] | ||
pub enum LicenseField { | ||
LicenseList(LicenseList), | ||
License(License), | ||
// In very rare cases the license is just a String. | ||
// This mostly serves as a fallback so that serde doesn't panic. | ||
String(String), | ||
} | ||
|
||
impl LicenseField { | ||
pub fn into_vec(self) -> Vec<License> { | ||
match self { | ||
Self::LicenseList(license_list) => license_list.0, | ||
Self::License(license) => vec![license], | ||
// Fallback to handle very unusual license fields in Nix. | ||
_ => vec![], | ||
impl BuildtimeInput { | ||
pub fn from_file(path: &Path) -> Result<Self> { | ||
let buildtime_input_json: Vec<Derivation> = serde_json::from_reader( | ||
fs::File::open(path).with_context(|| format!("Failed to open {path:?}"))?, | ||
) | ||
.context("Failed to parse buildtime input")?; | ||
let mut m = HashMap::new(); | ||
for derivation in buildtime_input_json { | ||
m.insert(derivation.path.clone(), derivation); | ||
} | ||
Ok(Self(m)) | ||
} | ||
} | ||
|
||
#[derive(Deserialize, Clone, Debug)] | ||
pub struct LicenseList(Vec<License>); | ||
|
||
#[derive(Deserialize, Clone, Debug)] | ||
pub struct License { | ||
#[serde(rename = "fullName")] | ||
pub full_name: String, | ||
#[serde(rename = "spdxId")] | ||
pub spdx_id: Option<String>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use serde::Deserialize; | ||
|
||
#[derive(Deserialize, Clone, Debug, Default)] | ||
pub struct Derivation { | ||
pub path: String, | ||
pub name: Option<String>, | ||
pub pname: Option<String>, | ||
pub version: Option<String>, | ||
pub meta: Option<Meta>, | ||
} | ||
|
||
impl Derivation { | ||
pub fn new(store_path: &str) -> Self { | ||
// Because we only have the store path we have to derive the pname and version from it | ||
let stripped = store_path.strip_prefix("/nix/store/"); | ||
let pname = stripped | ||
.and_then(|s| s.split('-').nth(1)) | ||
.map(ToOwned::to_owned); | ||
let version = stripped | ||
.and_then(|s| s.split('-').last()) | ||
.map(ToOwned::to_owned); | ||
|
||
Self { | ||
path: store_path.to_string(), | ||
pname, | ||
version, | ||
..Self::default() | ||
} | ||
} | ||
} | ||
|
||
#[derive(Deserialize, Clone, Debug)] | ||
pub struct Meta { | ||
pub license: Option<LicenseField>, | ||
pub homepage: Option<String>, | ||
} | ||
|
||
#[derive(Deserialize, Clone, Debug)] | ||
#[serde(untagged)] | ||
pub enum LicenseField { | ||
LicenseList(LicenseList), | ||
License(License), | ||
// In very rare cases the license is just a String. | ||
// This mostly serves as a fallback so that serde doesn't panic. | ||
String(String), | ||
} | ||
|
||
impl LicenseField { | ||
pub fn into_vec(self) -> Vec<License> { | ||
match self { | ||
Self::LicenseList(license_list) => license_list.0, | ||
Self::License(license) => vec![license], | ||
// Fallback to handle very unusual license fields in Nix. | ||
_ => vec![], | ||
} | ||
} | ||
} | ||
|
||
#[derive(Deserialize, Clone, Debug)] | ||
pub struct LicenseList(Vec<License>); | ||
|
||
#[derive(Deserialize, Clone, Debug)] | ||
pub struct License { | ||
#[serde(rename = "fullName")] | ||
pub full_name: String, | ||
#[serde(rename = "spdxId")] | ||
pub spdx_id: Option<String>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
mod buildtime_input; | ||
mod cli; | ||
mod cyclonedx; | ||
mod derivation; | ||
mod runtime_input; | ||
mod transform; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters