From a98d1f89634f938d8c5e810fb4bb6e01ec9d3259 Mon Sep 17 00:00:00 2001 From: Zhaofeng Li Date: Tue, 16 Aug 2022 20:15:43 -0600 Subject: [PATCH] nix/flake: Always resolve flake metadata --- src/nix/flake.rs | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/nix/flake.rs b/src/nix/flake.rs index 01f2bfe..79e7232 100644 --- a/src/nix/flake.rs +++ b/src/nix/flake.rs @@ -12,13 +12,24 @@ use super::{ColmenaError, ColmenaResult, NixCheck}; /// A Nix Flake. #[derive(Debug)] pub struct Flake { - /// The Flake URI. - uri: String, + /// The flake metadata. + metadata: FlakeMetadata, /// The directory the flake lives in, if it's a local flake. local_dir: Option, } +/// 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 { /// Creates a flake from the given directory. /// @@ -33,10 +44,10 @@ impl Flake { .to_str() .expect("Flake directory path contains non-UTF-8 characters"); - let info = FlakeMetadata::resolve(flake).await?; + let metadata = FlakeMetadata::resolve(flake).await?; Ok(Self { - uri: info.resolved_url, + metadata, local_dir: Some(dir.as_ref().to_owned()), }) } @@ -45,15 +56,25 @@ impl Flake { pub async fn from_uri(uri: String) -> ColmenaResult { NixCheck::require_flake_support().await?; + let metadata = FlakeMetadata::resolve(&uri).await?; + Ok(Self { - uri, + metadata, local_dir: None, }) } /// Returns the URI. 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. @@ -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 { /// Resolves a flake. async fn resolve(flake: &str) -> ColmenaResult {