Move the systemsConfigs key so that every entry defines its system. Determine flake attrs automatically.

This commit is contained in:
R-VdP 2023-03-14 20:37:35 +01:00
parent 5ccc6b1bba
commit 15dd869682
No known key found for this signature in database
3 changed files with 51 additions and 10 deletions

View file

@ -100,17 +100,11 @@
};
in
{
systemConfig = self.lib.makeSystemConfig {
inherit system;
modules = [
./nix/modules
];
};
packages = {
inherit system-manager;
default = self.packages.${system}.system-manager;
};
devShells.default = pkgs.devshell.mkShell {
packages = with pkgs; [
llvm.clang
@ -174,6 +168,7 @@
};
}).shellHook;
};
checks = {
inherit
# Build the crate as part of `nix flake check` for convenience
@ -186,5 +181,12 @@
lib = import ./nix/lib.nix {
inherit nixpkgs self;
};
systemConfigs.default = self.lib.makeSystemConfig {
system = flake-utils.lib.system.x86_64-linux;
modules = [
./nix/modules
];
};
};
}

View file

@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::DirBuilder;
use std::path::Path;
use std::process::ExitStatus;
use std::{fs, process, str};
use super::{create_store_link, StorePath, FLAKE_ATTR, GCROOT_PATH, PROFILE_DIR, PROFILE_NAME};
@ -52,8 +53,7 @@ fn create_gcroot(gcroot_path: &str, profile_path: &Path) -> Result<()> {
}
pub fn build(flake_uri: &str) -> Result<StorePath> {
// FIXME: we should not hard-code the system here
let flake_attr = format!("{FLAKE_ATTR}.x86_64-linux");
let flake_attr = find_flake_attr(flake_uri)?;
log::info!("Building new system-manager generation...");
log::info!("Running nix build...");
@ -62,6 +62,34 @@ pub fn build(flake_uri: &str) -> Result<StorePath> {
Ok(store_path)
}
fn find_flake_attr(flake_uri: &str) -> Result<String> {
let hostname = nix::unistd::gethostname()?;
let flake_attr = format!("{FLAKE_ATTR}.{}", hostname.to_string_lossy());
let status = try_flake_attr(flake_uri, &flake_attr)?;
if status {
return Ok(flake_attr);
} else {
let flake_attr = format!("{FLAKE_ATTR}.default");
let status = try_flake_attr(flake_uri, &flake_attr)?;
if status {
return Ok(flake_attr);
};
};
anyhow::bail!("No suitable flake attribute found, giving up.");
}
fn try_flake_attr(flake_uri: &str, flake_attr: &str) -> Result<bool> {
log::info!("Trying flake attribute: {flake_uri}#{flake_attr}...");
let status = try_nix_eval(flake_uri, flake_attr)?;
if status.success() {
log::info!("Success, using {flake_uri}#{flake_attr}");
} else {
log::info!("Attribute {flake_uri}#{flake_attr} not found in flake.");
};
Ok(status.success())
}
fn get_store_path(nix_build_result: process::Output) -> Result<StorePath> {
if nix_build_result.status.success() {
String::from_utf8(nix_build_result.stdout)
@ -103,3 +131,14 @@ fn run_nix_build(flake_uri: &str, flake_attr: &str) -> Result<process::Output> {
.output()?;
Ok(output)
}
fn try_nix_eval(flake_uri: &str, flake_attr: &str) -> Result<process::ExitStatus> {
let status = process::Command::new("nix")
.arg("eval")
.arg(format!("{flake_uri}#{flake_attr}"))
.arg("--json")
.stdout(process::Stdio::null())
.stderr(process::Stdio::null())
.status()?;
Ok(status)
}

View file

@ -8,7 +8,7 @@ use std::os::unix;
use std::path::{Path, PathBuf};
use std::{fs, str};
const FLAKE_ATTR: &str = "systemConfig";
const FLAKE_ATTR: &str = "systemConfigs";
const PROFILE_DIR: &str = "/nix/var/nix/profiles/system-manager-profiles";
const PROFILE_NAME: &str = "system-manager";
const GCROOT_PATH: &str = "/nix/var/nix/gcroots/system-manager-current";