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

Fixes and improvements #115

Merged
merged 10 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Commands:

Options:
-d, --directory <FOLDER> Base folder for sources.json and the boilerplate default.nix [env: NPINS_DIRECTORY=] [default: npins]
-v, --verbose Print debug messages
-h, --help Print help
-V, --version Print version
```
Expand All @@ -138,6 +139,7 @@ Usage: npins init [OPTIONS]
Options:
--bare Don't add an initial `nixpkgs` entry
-d, --directory <FOLDER> Base folder for sources.json and the boilerplate default.nix [env: NPINS_DIRECTORY=] [default: npins]
-v, --verbose Print debug messages
-h, --help Print help
```

Expand Down Expand Up @@ -166,6 +168,7 @@ Arguments:
Options:
-d, --directory <FOLDER> Base folder for sources.json and the boilerplate default.nix [env: NPINS_DIRECTORY=] [default: npins]
-n, --name <NAME> Only import one entry from Niv
-v, --verbose Print debug messages
-h, --help Print help
```

Expand Down Expand Up @@ -208,6 +211,7 @@ Options:
-d, --directory <FOLDER> Base folder for sources.json and the boilerplate default.nix [env: NPINS_DIRECTORY=] [default: npins]
--name <NAME> Add the pin with a custom name. If a pin with that name already exists, it will be overwritten
-n, --dry-run Don't actually apply the changes
-v, --verbose Print debug messages
-h, --help Print help
```

Expand All @@ -231,6 +235,8 @@ Options:
Add the pin with a custom name. If a pin with that name already exists, it will be overwritten
--at <tag or rev>
Use a specific commit/release instead of the latest. This may be a tag name, or a git revision when --branch is set
-v, --verbose
Print debug messages
--pre-releases
Also track pre-releases. Conflicts with the --branch option
--upper-bound <version>
Expand All @@ -256,6 +262,7 @@ Arguments:

Options:
-d, --directory <FOLDER> Base folder for sources.json and the boilerplate default.nix [env: NPINS_DIRECTORY=] [default: npins]
-v, --verbose Print debug messages
-h, --help Print help
```

Expand All @@ -271,6 +278,7 @@ Usage: npins show [OPTIONS]

Options:
-d, --directory <FOLDER> Base folder for sources.json and the boilerplate default.nix [env: NPINS_DIRECTORY=] [default: npins]
-v, --verbose Print debug messages
-h, --help Print help
```

Expand All @@ -294,6 +302,8 @@ Options:
Don't update versions, only re-fetch hashes
-f, --full
Re-fetch hashes even if the version hasn't changed. Useful to make sure the derivations are in the Nix store
-v, --verbose
Print debug messages
-n, --dry-run
Print the diff, but don't write back the changes
--max-concurrent-downloads <MAX_CONCURRENT_DOWNLOADS>
Expand All @@ -314,6 +324,7 @@ Usage: npins upgrade [OPTIONS]

Options:
-d, --directory <FOLDER> Base folder for sources.json and the boilerplate default.nix [env: NPINS_DIRECTORY=] [default: npins]
-v, --verbose Print debug messages
-h, --help Print help
```

Expand Down
6 changes: 5 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,10 @@ pub struct Opts {
)]
folder: std::path::PathBuf,

/// Print debug messages.
#[arg(global = true, short = 'v', long = "verbose")]
pub verbose: bool,

#[command(subcommand)]
command: Command,
}
Expand Down Expand Up @@ -702,7 +706,7 @@ impl Opts {
}

let pin_writer = |mut name: StyledContent<&str>, status: &str, index: usize| {
if stderr().is_terminal() {
if stderr().is_terminal() && !self.verbose {
let seek_distance = (length - index) as u16;

execute!(
Expand Down
6 changes: 6 additions & 0 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ impl RemoteInfo {

/// Convenience wrapper around calling `git ls-remote`
async fn fetch_remote(args: &[&str]) -> Result<Vec<RemoteInfo>> {
log::debug!("Executing `git ls-remote {}`", args.join(" "));
let process = Command::new("git")
// Disable any interactive login attempts, failing gracefully instead
.env("GIT_TERMINAL_PROMPT", "0")
Expand All @@ -638,6 +639,10 @@ async fn fetch_remote(args: &[&str]) -> Result<Vec<RemoteInfo>> {
.unwrap_or_else(|| "None".into())
);
}
log::debug!("git ls-remote stdout:");
String::from_utf8_lossy(&process.stdout)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this probably works it wouldn't help a lot of git is stuck at some point during the execution. Since this is already async we could log lines as they appear? Probably something for the future but I'm usually deeply frustrated by applications that eat the error output (we only provide stdout) and that block until the subprocess is finished. Usually I just attach strace and check again, but nobody should be required to resort to that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can look into it in a follow up. Ideally we'd factor out the process of shelling out into a helper function which does the appropriate wrapping, so that id doesn't have to be repeated at every call site (even though currently there are only two). In the case of git specifically, we could maybe also look into using libgit or any other Rust git bindings to call the code directly

.split('\n')
.for_each(|line| log::debug!("> {}", line));

String::from_utf8_lossy(&process.stdout)
.split('\n')
Expand All @@ -650,6 +655,7 @@ async fn fetch_remote(args: &[&str]) -> Result<Vec<RemoteInfo>> {
!ref_.contains('\t'),
"Output line contains more than one '\\t'"
);
log::debug!("Found remote: {}, {}", revision, ref_);
Ok(RemoteInfo {
revision: revision.into(),
ref_: ref_.into(),
Expand Down
9 changes: 7 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,18 @@ impl diff::Diff for GenericUrlHashes {

#[tokio::main]
async fn main() -> Result<()> {
let opts = cli::Opts::parse();

env_logger::builder()
.filter_level(log::LevelFilter::Info)
.filter_level(if opts.verbose {
log::LevelFilter::Debug
} else {
log::LevelFilter::Info
})
.format_timestamp(None)
.format_target(false)
.init();

let opts = cli::Opts::parse();
opts.run().await?;
Ok(())
}
20 changes: 20 additions & 0 deletions src/nix.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::{Context, Result};
use log::debug;

#[allow(unused)]
pub struct PrefetchInfo {
Expand All @@ -8,6 +9,10 @@ pub struct PrefetchInfo {

pub async fn nix_prefetch_tarball(url: impl AsRef<str>) -> Result<String> {
let url = url.as_ref();
log::debug!(
"Executing `nix-prefetch-url --unpack --type sha256 {}`",
url
);
let output = tokio::process::Command::new("nix-prefetch-url")
.arg("--unpack") // force calculation of the unpacked NAR hash
.arg("--type")
Expand All @@ -27,6 +32,7 @@ pub async fn nix_prefetch_tarball(url: impl AsRef<str>) -> Result<String> {
}

let stdout = String::from_utf8_lossy(&output.stdout);
log::debug!("Got hash: {}", stdout);
Ok(String::from(stdout.trim()))
}

Expand All @@ -36,6 +42,16 @@ pub async fn nix_prefetch_git(
submodules: bool,
) -> Result<String> {
let url = url.as_ref();
log::debug!(
"Executing: `nix-prefetch-git {}{} {}`",
if submodules {
"--fetch-submodules "
} else {
""
},
url,
git_ref.as_ref()
);
let mut output = tokio::process::Command::new("nix-prefetch-git");
if submodules {
output.arg("--fetch-submodules");
Expand Down Expand Up @@ -80,6 +96,10 @@ pub async fn nix_prefetch_git(
leave_dot_git: bool,
}

debug!(
"nix-prefetch-git output: {}",
String::from_utf8_lossy(&output.stdout)
);
let info: NixPrefetchGitResponse = serde_json::from_slice(&output.stdout)
.context("Failed to deserialize nix-pfetch-git JSON response.")?;

Expand Down