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

Try multiple file paths for netrc file #149

Open
wants to merge 8 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 = "fh"
version = "0.1.18"
version = "0.1.19"
authors = ["Determinate Systems <[email protected]>"]
edition = "2021"
license = "Apache 2.0"
Expand Down
26 changes: 13 additions & 13 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/cli/cmd/login/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use hyper_util::rt::TokioIo;
use tokio::io::AsyncWriteExt;
use tokio::net::UnixStream;

use crate::cli::cmd::FlakeHubClient;
use crate::cli::cmd::TokenStatus;
use crate::cli::cmd::{get_netrc_path, FlakeHubClient};
use crate::cli::error::FhError;
use crate::shared::{update_netrc_file, NetrcTokenAddRequest};
use crate::{DETERMINATE_NIXD_SOCKET_NAME, DETERMINATE_STATE_DIR};
Expand Down Expand Up @@ -191,7 +191,7 @@ impl LoginSubcommand {

let xdg = xdg::BaseDirectories::new()?;

let netrc_path = xdg.place_config_file("nix/netrc")?;
let netrc_path = get_netrc_path(&xdg).await?;

// $XDG_CONFIG_HOME/nix/nix.conf; basically ~/.config/nix/nix.conf
let nix_config_path = xdg.place_config_file("nix/nix.conf")?;
Expand Down
23 changes: 22 additions & 1 deletion src/cli/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub(crate) mod resolve;
pub(crate) mod search;
pub(crate) mod status;

use std::{fmt::Display, process::Stdio};
use std::{fmt::Display, path::PathBuf, process::Stdio};

use color_eyre::eyre::WrapErr;
use once_cell::sync::Lazy;
Expand All @@ -23,7 +23,9 @@ use tabled::settings::{
style::{HorizontalLine, On, VerticalLineIter},
Style,
};
use tokio::fs::try_exists;
use url::Url;
use xdg::BaseDirectories;

use self::{
init::command_exists,
Expand All @@ -36,6 +38,9 @@ use crate::{flakehub_url, APP_USER_AGENT};

use super::error::FhError;

const DETERMINATE_NIX_NETRC_PATH: &str = "/nix/var/determinate/netrc";
const XDG_NIX_NETRC_SUFFIX: &str = "nix/netrc";

#[allow(clippy::type_complexity)]
static DEFAULT_STYLE: Lazy<
Style<
Expand Down Expand Up @@ -477,6 +482,22 @@ fn validate_segment(s: &str) -> Result<(), FhError> {
Ok(())
}

// See if the netrc exists at the /nix/var/determinate/netrc and, if not, try
// to find it via XDG path.
async fn get_netrc_path(xdg: &BaseDirectories) -> Result<PathBuf, FhError> {
Copy link
Member

Choose a reason for hiding this comment

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

I kinda wonder about this... I think we probably need two separate functions: one for when we're getting a path-to-write-to (i.e. the user running fh login without determinate-nixd running), and one for when we're getting a path-to-read-from.

In the write-to one, it would take into account if determinate-nixd is running and return None if it is (since it won't write to a file, but will instead tell the daemon) and Some(xdg-path) if it's not.

In the read-from one, it would return netrcnotfound if the determinate and normal netrc files are not found.

Copy link
Member Author

Choose a reason for hiding this comment

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

What's the canonical way to determine if dnixd is running?

Copy link
Member

Choose a reason for hiding this comment

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

I think the dnixd_uds function is a good starting point, but basically request to http://localhost/info and see if it responds with 200.

Copy link
Member Author

Choose a reason for hiding this comment

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

Cool cool, shouldn't be too bad

Copy link
Member Author

Choose a reason for hiding this comment

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

@cole-h What action do we take if the write-to path is None?

Copy link
Member

Choose a reason for hiding this comment

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

I imagine that case is only reachable when we're using dnixd, so we just wouldn't write a file and tell the daemon to use this token the same way we do it already.

match try_exists(DETERMINATE_NIX_NETRC_PATH).await {
Ok(exists) if exists => Ok(DETERMINATE_NIX_NETRC_PATH.into()),
_ => {
let xdg_path = xdg.place_config_file(XDG_NIX_NETRC_SUFFIX)?;

match try_exists(&xdg_path).await {
Ok(exists) if exists => Ok(xdg_path),
_ => Err(FhError::NetrcNotFound(xdg_path.display().to_string())),
}
}
}
}

#[cfg(test)]
mod tests {
#[test]
Expand Down
3 changes: 3 additions & 0 deletions src/cli/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ pub(crate) enum FhError {
#[error("missing from flake output reference: {0}")]
MissingFromOutputRef(String),

#[error("netrc file not found at {0}")]
NetrcNotFound(String),

#[error("the flake has no inputs")]
NoInputs,

Expand Down
Loading