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.
#[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<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 {
/// 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<Self> {
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<Self> {