nix/flake: Always resolve flake metadata

This commit is contained in:
Zhaofeng Li 2022-08-16 20:15:43 -06:00
parent 5e76e8ab26
commit a98d1f8963

View file

@ -12,13 +12,24 @@ use super::{ColmenaError, ColmenaResult, NixCheck};
/// A Nix Flake. /// A Nix Flake.
#[derive(Debug)] #[derive(Debug)]
pub struct Flake { pub struct Flake {
/// The Flake URI. /// The flake metadata.
uri: String, metadata: FlakeMetadata,
/// The directory the flake lives in, if it's a local flake. /// The directory the flake lives in, if it's a local flake.
local_dir: Option<PathBuf>, local_dir: Option<PathBuf>,
} }
/// A `nix flake metadata --json` invocation.
#[derive(Deserialize, Debug)]
struct FlakeMetadata {
/// The resolved URL of the flake.
#[serde(rename = "resolvedUrl")]
resolved_url: String,
/// The locked URL of the flake.
url: String,
}
impl Flake { impl Flake {
/// Creates a flake from the given directory. /// Creates a flake from the given directory.
/// ///
@ -33,10 +44,10 @@ impl Flake {
.to_str() .to_str()
.expect("Flake directory path contains non-UTF-8 characters"); .expect("Flake directory path contains non-UTF-8 characters");
let info = FlakeMetadata::resolve(flake).await?; let metadata = FlakeMetadata::resolve(flake).await?;
Ok(Self { Ok(Self {
uri: info.resolved_url, metadata,
local_dir: Some(dir.as_ref().to_owned()), local_dir: Some(dir.as_ref().to_owned()),
}) })
} }
@ -45,15 +56,25 @@ impl Flake {
pub async fn from_uri(uri: String) -> ColmenaResult<Self> { pub async fn from_uri(uri: String) -> ColmenaResult<Self> {
NixCheck::require_flake_support().await?; NixCheck::require_flake_support().await?;
let metadata = FlakeMetadata::resolve(&uri).await?;
Ok(Self { Ok(Self {
uri, metadata,
local_dir: None, local_dir: None,
}) })
} }
/// Returns the URI. /// Returns the URI.
pub fn uri(&self) -> &str { pub fn uri(&self) -> &str {
&self.uri &self.metadata.resolved_url
}
/// Returns the locked URI.
///
/// Note that the URI will not be locked if the git workspace
/// is dirty.
pub fn locked_uri(&self) -> &str {
&self.metadata.url
} }
/// Returns the local directory, if it exists. /// Returns the local directory, if it exists.
@ -62,14 +83,6 @@ impl Flake {
} }
} }
/// A `nix flake metadata --json` invocation.
#[derive(Deserialize, Debug)]
struct FlakeMetadata {
/// The resolved URL of the flake.
#[serde(rename = "resolvedUrl")]
resolved_url: String,
}
impl FlakeMetadata { impl FlakeMetadata {
/// Resolves a flake. /// Resolves a flake.
async fn resolve(flake: &str) -> ColmenaResult<Self> { async fn resolve(flake: &str) -> ColmenaResult<Self> {