Skip to content

Commit

Permalink
feat: add option to only extract runtime dependencies
Browse files Browse the repository at this point in the history
Where runtime dependencies are defined as those in `buildInputs` and `propagatedBuildInputs`
  • Loading branch information
Erin van der Veen committed Mar 12, 2024
1 parent 6581482 commit 1d6acf4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub struct ProcessingArgs<'a> {
pub attribute_path: String,
pub offline: bool,
pub include_nar_info: bool,
pub runtime_only: bool,
pub binary_caches: &'a Vec<String>,
pub lib: &'a nix::lib::Lib,
pub tx: mpsc::Sender<DerivationDescription>,
Expand All @@ -62,6 +63,7 @@ fn process(args: ProcessingArgs) -> Result<()> {
args.system,
&args.attribute_path,
&args.offline,
&args.runtime_only,
&args.include_nar_info,
args.binary_caches,
args.lib,
Expand Down Expand Up @@ -116,6 +118,7 @@ pub fn nixtract(
attribute_path: Option<impl Into<String>>,
offline: bool,
include_nar_info: bool,
runtime_only: bool,
binary_caches: Option<Vec<String>>,
) -> Result<impl Iterator<Item = DerivationDescription>> {
// Convert the arguments to the expected types
Expand Down Expand Up @@ -165,6 +168,7 @@ pub fn nixtract(
system: &system,
attribute_path: found_drv.attribute_path,
offline,
runtime_only,
include_nar_info,
binary_caches: &binary_caches,
lib: &lib,
Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ struct Args {
#[arg(long, default_value_t = false)]
output_schema: bool,

/// Ouput only runtime dependencies
#[arg(long, short, default_value_t = false)]
runtime_only: bool,

/// Write the output to a file instead of stdout or explicitly use `-` for stdout
#[arg()]
output_path: Option<String>,
Expand Down Expand Up @@ -116,6 +120,7 @@ fn main_with_args(opts: Args) -> Result<(), Box<dyn Error>> {
opts.attribute_path,
opts.offline,
opts.include_nar_info,
opts.runtime_only,
opts.binary_caches,
)?;

Expand Down Expand Up @@ -175,6 +180,7 @@ mod tests {
// Write output to /dev/null to avoid cluttering the test output
output_path: Some("/dev/null".to_string()),
include_nar_info: false,
runtime_only: false,
binary_caches: None,
};

Expand Down
8 changes: 7 additions & 1 deletion src/nix/describe_derivation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# TARGET_FLAKE_REF: flake reference to evaluate
# TARGET_SYSTEM: system to evaluate
# TARGET_ATTRIBUTE_PATH: attribute path to the derivation to evaluate
# RUNTIME_ONLY: 1 if you only want to include "buildInputs" (only runtime dependencies), 0 if you want all dependencies
#
# Example:
# TARGET_FLAKE_REF="nixpkgs" TARGET_SYSTEM="x86_64-linux" TARGET_ATTRIBUTE_PATH="python3" nix eval --json --file describe-derivation.nix
Expand All @@ -16,6 +17,8 @@ let
targetFlakeRef = builtins.getEnv "TARGET_FLAKE_REF";
targetAttributePath = builtins.getEnv "TARGET_ATTRIBUTE_PATH";
targetSystem = let env = builtins.getEnv "TARGET_SYSTEM"; in if env == "" then builtins.currentSystem else env;
# 0 is false, everything else is true
runtimeOnly = if builtins.getEnv "RUNTIME_ONLY" == "0" then false else true;

# Get pkgs
targetFlake = builtins.getFlake targetFlakeRef;
Expand Down Expand Up @@ -86,6 +89,9 @@ in
(lib.enumerate (targetValue.${inputType} or [ ]))
)
)
[ "nativeBuildInputs" "buildInputs" "propagatedBuildInputs" ]
(
[ "buildInputs" "propagatedBuildInputs" ]
++ nixpkgs.lib.optionals (!runtimeOnly) [ "nativeBuildInputs" ]
)
);
}
5 changes: 5 additions & 0 deletions src/nix/describe_derivation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub fn describe_derivation(
system: &Option<String>,
attribute_path: &String,
offline: &bool,
runtime_only: &bool,
include_nar_info: &bool,
binary_caches: &[String],
lib: &Lib,
Expand All @@ -88,6 +89,10 @@ pub fn describe_derivation(
("NIXPKGS_ALLOW_UNFREE".to_owned(), "1".to_owned()),
("NIXPKGS_ALLOW_INSECURE".to_owned(), "1".to_owned()),
("NIXPKGS_ALLOW_BROKEN".to_owned(), "1".to_owned()),
(
"RUNTIME_ONLY".to_owned(),
if *runtime_only { "1" } else { "0" }.to_owned(),
),
]);
if let Some(system) = system {
res.insert("TARGET_SYSTEM".to_owned(), system.to_owned());
Expand Down

0 comments on commit 1d6acf4

Please sign in to comment.