difftreelog
refactor declare configuration using flake parts
in: trunk
29 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1432,6 +1432,7 @@
name = "nix-eval"
version = "0.1.0"
dependencies = [
+ "anyhow",
"better-command",
"futures",
"itertools",
README.adocdiffbeforeafterboth--- a/README.adoc
+++ b/README.adoc
@@ -24,44 +24,33 @@
url = "github:CertainLach/fleet";
inputs.nixpkgs.follows = "nixpkgs";
};
+ flake-parts.url = "github:hercules-ci/flake-parts";
lanzaboote = {
url = "github:nix-community/lanzaboote/v0.3.0";
inputs.nixpkgs.follows = "nixpkgs";
};
};
- outputs = {
- nixpkgs,
- fleet,
- lanzaboote,
- ...
- }: {
- # TODO: This section of documentation needs to use flake-utils.
- formatter.x86_64-linux = let
- pkgs = import nixpkgs {system = "x86_64-linux";};
- in
- pkgs.alejandra;
+ outputs = inputs: flake-parts.lib.mkFlake { inherit inputs; } {
+ imports = [inputs.fleet.flakeModules.default];
- devShell.x86_64-linux = let
- pkgs = import nixpkgs {
- system = "x86_64-linux";
- };
- in
- pkgs.mkShell {
- buildInputs = with pkgs; [
- fleet.packages.x86_64-linux.fleet
+ perSystem = {pkgs, system, ...}: {
+ _module.args.pkgs = import nixpkgs { inherit system; };
+
+ formatter = pkgs.alejandra;
+ devShells.default = pkgs.mkShell {
+ packages = [
+ inputs.fleet.packages.${system}.fleet
];
};
+ };
# Single flake may contain multiple fleet configurations, default one is called... `default`
- fleetConfigurations.default = fleet.lib.fleetConfiguration {
+ fleetConfigurations.default = {
# nixpkgs used to build the systems
- inherit nixpkgs;
- # fleet wants to pass some data, like secrets, to do that - fleet writes all the encrypted secrets to fleet.nix
- # treat the contents of this file as implementation detail
- data = import ./fleet.nix;
+ nixpkgs.buildUsing = nixpkgs;
- # nixosModules section of fleet config declares modules, which are used for all configured nixos hosts.
- nixosModules = [
+ # nixos option section of fleet config declares module, which is used for all configured nixos hosts.
+ nixos.imports = [
lanzaboote.nixosModules.lanzaboote
{
# Make `nix shell nixpkgs#thing` use the same nixpkgs, as used to build the system.
@@ -77,7 +66,7 @@
# Is I.e wiring up the mesh VPN, or deploying kubernetes, or other things.
#
# Modules use the same semantics as standard nixos module system, they are just configuring all the hosts at once.
- fleetModules = [
+ imports = [
./wireguard
# Multi-instancible modules example
(import ./kubernetes {hosts = ["a" "b"];})
@@ -89,7 +78,7 @@
# Every host has some system, for which the system configuration needs to be built
system = "x86_64-linux";
# And nixos modules
- nixosModules = [
+ nixos.imports = [
./controlplane-1/hardware-configuration.nix
./controlplane-1/configuration.nix
# Configuration may also be specified inline, as in any nixos config.
cmds/fleet/src/cmds/build_systems.rsdiffbeforeafterboth--- a/cmds/fleet/src/cmds/build_systems.rs
+++ b/cmds/fleet/src/cmds/build_systems.rs
@@ -254,13 +254,8 @@
let host = config.host(&host).await?;
// let action = Action::from(self.subcommand.clone());
let fleet_config = &config.config_field;
- let drv = nix_go!(
- fleet_config.hosts[{ &host.name }]
- .nixosSystem
- .config
- .system
- .build[{ build_attr }]
- );
+ let nixos = host.nixos_config().await?;
+ let drv = nix_go!(nixos.system.build[{ build_attr }]);
let outputs = drv.build().await.inspect_err(|_| {
if build_attr == "sdImage" {
info!("sd-image build failed");
@@ -335,6 +330,7 @@
let config = config.clone();
let span = info_span!("deploy", host = field::display(&host.name));
let hostname = host.name.clone();
+ let local_host = config.local_host();
// FIXME: Fix repl concurrency (see build-systems)
set.spawn_local(
(async move {
@@ -354,7 +350,10 @@
// at least for the first deployment, to provide trusted store key.
//
// It is much slower, yet doesn't require root on the deployer machine.
- let mut sign = MyCommand::new("nix");
+ let Ok(mut sign) = local_host.cmd("nix").await else {
+ error!("failed to setup local");
+ return;
+ };
// Private key for host machine is registered in nix-sign.nix
sign.arg("store")
.arg("sign")
@@ -362,7 +361,7 @@
.arg("-r")
.arg(&built);
if let Err(e) = sign.sudo().run_nix().await {
- warn!("Failed to sign store paths: {e}");
+ warn!("failed to sign store paths: {e}");
};
}
let mut tries = 0;
cmds/fleet/src/cmds/info.rsdiffbeforeafterboth--- a/cmds/fleet/src/cmds/info.rs
+++ b/cmds/fleet/src/cmds/info.rs
@@ -38,9 +38,9 @@
InfoCmd::ListHosts { ref tagged } => {
'host: for host in config.list_hosts().await? {
if !tagged.is_empty() {
- let config = &config.config_unchecked_field;
+ let config = &config.config_field;
let tags: Vec<String> =
- nix_go_json!(config.hosts[{ host.name }].nixosSystem.config.tags);
+ nix_go_json!(config.hosts[{ host.name }].tags);
for tag in tagged {
if !tags.contains(tag) {
continue 'host;
cmds/fleet/src/cmds/secrets/mod.rsdiffbeforeafterboth1use std::{2 collections::{BTreeMap, BTreeSet, HashSet},3 ffi::OsString,4 io::{self, stdin, stdout, Read, Write},5 path::PathBuf,6};78use anyhow::{anyhow, bail, ensure, Context, Result};9use chrono::{DateTime, Utc};10use clap::Parser;11use crossterm::{terminal, tty::IsTty};12use fleet_shared::SecretData;13use itertools::Itertools;14use nix_eval::{nix_go, nix_go_json, Value};15use owo_colors::OwoColorize;16use serde::Deserialize;17use tabled::{Table, Tabled};18use tokio::{fs::read, process::Command};19use tracing::{error, info, info_span, warn, Instrument};2021use crate::{22 fleetdata::{encrypt_secret_data, FleetSecret, FleetSecretPart, FleetSharedSecret},23 host::Config,24};2526#[derive(Parser)]27pub enum Secret {28 /// Force load host keys for all defined hosts29 ForceKeys,30 /// Add secret, data should be provided in stdin31 AddShared {32 /// Secret name33 name: String,34 /// Secret owners35 #[clap(long, short)]36 machines: Vec<String>,37 /// Override secret if already present38 #[clap(long)]39 force: bool,40 /// Secret public part41 #[clap(long)]42 public: Option<String>,43 /// Load public part from specified file44 #[clap(long)]45 public_file: Option<PathBuf>,4647 /// Create a notification on secret expiration48 #[clap(long)]49 expires_at: Option<DateTime<Utc>>,5051 /// Secret with this name already exists, override its value while keeping the same owners.52 #[clap(long)]53 re_add: bool,5455 /// How to name public secret part56 #[clap(long, short = 'p', default_value = "public")]57 public_part: String,58 /// How to name private secret part59 #[clap(short = 's', long, default_value = "secret")]60 part: String,61 },62 /// Add secret, data should be provided in stdin63 Add {64 /// Secret name65 name: String,66 /// Secret owner67 #[clap(short = 'm', long)]68 machine: String,69 /// Replace secret if already present70 #[clap(long)]71 replace: bool,72 /// Add new parts to existing secret73 #[clap(long)]74 merge: bool,75 /// Secret public part76 #[clap(long)]77 public: Option<String>,78 /// Load public part from specified file79 #[clap(long)]80 public_file: Option<PathBuf>,8182 /// How to name public secret part83 #[clap(short = 'p', long, default_value = "public")]84 public_part: String,85 /// How to name private secret part86 #[clap(short = 's', long, default_value = "secret")]87 part: String,88 },89 /// Read secret from remote host, requires sudo on said host90 Read {91 name: String,92 #[clap(short = 'm', long)]93 machine: String,9495 /// Which private secret part to read96 #[clap(short = 'p', long, default_value = "secret")]97 part: String,98 },99 UpdateShared {100 name: String,101102 #[clap(short = 'm', long)]103 machine: Option<Vec<String>>,104105 #[clap(long)]106 add_machine: Vec<String>,107 #[clap(long)]108 remove_machine: Vec<String>,109110 /// Which host should we use to decrypt111 #[clap(long)]112 prefer_identities: Vec<String>,113 },114 Regenerate {115 /// Which host should we use to decrypt, in case if reencryption is required, without116 /// regeneration117 #[clap(long)]118 prefer_identities: Vec<String>,119 },120 List {},121 Edit {122 name: String,123 #[clap(short = 'm', long)]124 machine: String,125126 #[clap(long)]127 add: bool,128129 /// Which private secret part to read130 #[clap(short = 'p', long, default_value = "secret")]131 part: String,132 },133}134135#[tracing::instrument(skip(config, secret, field, prefer_identities))]136async fn update_owner_set(137 secret_name: &str,138 config: &Config,139 mut secret: FleetSharedSecret,140 field: Value,141 updated_set: &[String],142 prefer_identities: &[String],143) -> Result<FleetSharedSecret> {144 let original_set = secret.owners.clone();145146 let set = original_set.iter().collect::<BTreeSet<_>>();147 let expected_set = updated_set.iter().collect::<BTreeSet<_>>();148149 if set == expected_set {150 info!("no need to update owner list, it is already correct");151 return Ok(secret);152 }153154 let should_regenerate = if set.difference(&expected_set).next().is_some() {155 // TODO: Remove this warning for revokable secrets.156 warn!("host was removed from secret owners, but until this host rebuild, the secret will still be stored on it.");157 nix_go_json!(field.regenerateOnOwnerRemoved)158 } else if expected_set.difference(&set).next().is_some() {159 nix_go_json!(field.regenerateOnOwnerAdded)160 } else {161 false162 };163164 if should_regenerate {165 info!("secret is owner-dependent, will regenerate");166 let generated = generate_shared(config, secret_name, field, updated_set.to_vec()).await?;167 Ok(generated)168 } else {169 let identity_holder = if !prefer_identities.is_empty() {170 prefer_identities171 .iter()172 .find(|i| original_set.iter().any(|s| s == *i))173 } else {174 secret.owners.first()175 };176 let Some(identity_holder) = identity_holder else {177 bail!("no available holder found");178 };179180 for (part_name, part) in secret.secret.parts.iter_mut() {181 let _span = info_span!("part reencryption", part_name);182 if !part.raw.encrypted {183 continue;184 }185 let host = config.host(identity_holder).await?;186 let encrypted = host187 .reencrypt(part.raw.clone(), updated_set.to_vec())188 .await?;189 part.raw = encrypted;190 }191192 secret.owners = updated_set.to_vec();193 Ok(secret)194 }195}196197#[derive(Deserialize)]198#[serde(rename_all = "camelCase")]199enum GeneratorKind {200 Impure,201 Pure,202}203204async fn generate_pure(205 _config: &Config,206 _display_name: &str,207 _secret: Value,208 _default_generator: Value,209 _owners: &[String],210) -> Result<FleetSecret> {211 bail!("pure generators are broken for now")212}213async fn generate_impure(214 config: &Config,215 _display_name: &str,216 secret: Value,217 default_generator: Value,218 owners: &[String],219) -> Result<FleetSecret> {220 let generator = nix_go!(secret.generator);221 let on: Option<String> = nix_go_json!(default_generator.impureOn);222223 let host = if let Some(on) = &on {224 config.host(on).await?225 } else {226 config.local_host()227 };228 let on_pkgs = host.pkgs().await?;229 let call_package = nix_go!(on_pkgs.callPackage);230 let mk_secret_generators = nix_go!(on_pkgs.mkSecretGenerators);231232 let mut recipients = Vec::new();233 for owner in owners {234 let key = config.key(owner).await?;235 recipients.push(key);236 }237 let generators = nix_go!(mk_secret_generators(Obj {238 recipients: { recipients },239 }));240241 let generator = nix_go!(call_package(generator)(generators));242243 let generator = generator.build().await?;244 let generator = generator245 .get("out")246 .ok_or_else(|| anyhow!("missing generateImpure out"))?;247 let generator = host.remote_derivation(generator).await?;248249 let out_parent = host.mktemp_dir().await?;250 let out = format!("{out_parent}/out");251252 let mut gen = host.cmd(generator).await?;253 gen.env("out", &out);254 if on.is_none() {255 // This path is local, thus we can feed `OsString` directly to env var... But I don't think that's necessary to handle.256 let project_path: String = config257 .directory258 .clone()259 .into_os_string()260 .into_string()261 .map_err(|s| anyhow!("fleet project path is not utf-8: {s:?}"))?;262 gen.env("FLEET_PROJECT", project_path);263 }264 gen.run().await.context("impure generator")?;265266 {267 let marker = host.read_file_text(format!("{out}/marker")).await?;268 ensure!(marker == "SUCCESS", "generation not succeeded");269 }270271 let mut parts = BTreeMap::new();272 for part in host.read_dir(&out).await? {273 if part == "created_at" || part == "expired_at" || part == "marker" {274 continue;275 }276 let contents: SecretData = host277 .read_file_text(format!("{out}/{part}"))278 .await?279 .parse()280 .map_err(|e| anyhow!("failed to decode secret {out:?} part {part:?}: {e}"))?;281 parts.insert(part.to_owned(), FleetSecretPart { raw: contents });282 }283284 let created_at = host.read_file_value(format!("{out}/created_at")).await?;285 let expires_at = host.read_file_value(format!("{out}/expires_at")).await.ok();286287 Ok(FleetSecret {288 created_at,289 expires_at,290 parts,291 })292}293async fn generate(294 config: &Config,295 display_name: &str,296 secret: Value,297 owners: &[String],298) -> Result<FleetSecret> {299 let generator = nix_go!(secret.generator);300 // Can't properly check on nix module system level301 {302 let gen_ty = generator.type_of().await?;303 if gen_ty == "null" {304 bail!("secret has no generator defined, can't automatically generate it.");305 }306 if gen_ty != "lambda" {307 bail!("generator should be lambda, got {gen_ty}");308 }309 }310 let default_pkgs = &config.default_pkgs;311 let default_call_package = nix_go!(default_pkgs.callPackage);312 let default_mk_secret_generators = nix_go!(default_pkgs.mkSecretGenerators);313 // Generators provide additional information in passthru, to access314 // passthru we should call generator, but information about where this generator is supposed to build315 // is located in passthru... Thus evaluating generator on host.316 //317 // Maybe it is also possible to do some magic with __functor?318 //319 // I don't want to make modules always responsible for additional secret data anyway,320 // so it should be in derivation, and not in the secret data itself.321 let generators = nix_go!(default_mk_secret_generators(Obj {322 recipients: { <Vec<String>>::new() },323 }));324 let default_generator = nix_go!(default_call_package(generator)(generators));325326 let kind: GeneratorKind = nix_go_json!(default_generator.generatorKind);327328 match kind {329 GeneratorKind::Impure => {330 generate_impure(config, display_name, secret, default_generator, owners).await331 }332 GeneratorKind::Pure => {333 generate_pure(config, display_name, secret, default_generator, owners).await334 }335 }336}337async fn generate_shared(338 config: &Config,339 display_name: &str,340 secret: Value,341 expected_owners: Vec<String>,342) -> Result<FleetSharedSecret> {343 // let owners: Vec<String> = nix_go_json!(secret.expectedOwners);344 Ok(FleetSharedSecret {345 secret: generate(config, display_name, secret, &expected_owners).await?,346 owners: expected_owners,347 })348}349350async fn parse_public(351 public: Option<String>,352 public_file: Option<PathBuf>,353) -> Result<Option<SecretData>> {354 Ok(match (public, public_file) {355 (Some(v), None) => Some(SecretData {356 data: v.into(),357 encrypted: false,358 }),359 (None, Some(v)) => Some(SecretData {360 data: read(v).await?,361 encrypted: false,362 }),363 (Some(_), Some(_)) => {364 bail!("only public or public_file should be set")365 }366 (None, None) => None,367 })368}369370async fn parse_secret() -> Result<Option<Vec<u8>>> {371 let mut input = vec![];372 stdin().read_to_end(&mut input)?;373 if input.is_empty() {374 Ok(None)375 } else {376 Ok(Some(input))377 }378}379380fn parse_machines(381 initial: Vec<String>,382 machines: Option<Vec<String>>,383 mut add_machines: Vec<String>,384 mut remove_machines: Vec<String>,385) -> Result<Vec<String>> {386 if machines.is_none() && add_machines.is_empty() && remove_machines.is_empty() {387 bail!("no operation");388 }389390 let initial_machines = initial.clone();391 let mut target_machines = initial;392 info!("Currently encrypted for {initial_machines:?}");393394 // ensure!(machines.is_some() || !add_machines.is_empty() || )395 if let Some(machines) = machines {396 ensure!(397 add_machines.is_empty() && remove_machines.is_empty(),398 "can't combine --machines and --add-machines/--remove-machines"399 );400 let target = initial_machines.iter().collect::<HashSet<_>>();401 let source = machines.iter().collect::<HashSet<_>>();402 for removed in target.difference(&source) {403 remove_machines.push((*removed).clone());404 }405 for added in source.difference(&target) {406 add_machines.push((*added).clone());407 }408 }409410 for machine in &remove_machines {411 let mut removed = false;412 while let Some(pos) = target_machines.iter().position(|m| m == machine) {413 target_machines.swap_remove(pos);414 removed = true;415 }416 if !removed {417 warn!("secret is not enabled for {machine}");418 }419 }420 for machine in &add_machines {421 if target_machines.iter().any(|m| m == machine) {422 warn!("secret is already added to {machine}");423 } else {424 target_machines.push(machine.to_owned());425 }426 }427 if !remove_machines.is_empty() {428 // TODO: maybe force secret regeneration?429 // Not that useful without revokation.430 warn!("secret will not be regenerated for removed machines, and until host rebuild, they will still possess the ability to decode secret");431 }432 Ok(target_machines)433}434impl Secret {435 pub async fn run(self, config: &Config) -> Result<()> {436 match self {437 Secret::ForceKeys => {438 for host in config.list_hosts().await? {439 if config.should_skip(&host).await? {440 continue;441 }442 config.key(&host.name).await?;443 }444 }445 Secret::AddShared {446 mut machines,447 name,448 force,449 public,450 public_part: public_name,451 public_file,452 expires_at,453 re_add,454 part: part_name,455 } => {456 // TODO: Forbid updating secrets with set expectedOwners (= not user-managed).457458 let exists = config.has_shared(&name);459 if exists && !force && !re_add {460 bail!("secret already defined");461 }462 if re_add {463 // Fixme: use clap to limit this usage464 ensure!(!force, "--force and --readd are not compatible");465 ensure!(exists, "secret doesn't exists");466 ensure!(467 machines.is_empty(),468 "you can't use machines argument for --readd"469 );470 let shared = config.shared_secret(&name)?;471 machines = shared.owners;472 }473474 let recipients = config.recipients(machines.clone()).await?;475476 let mut parts = BTreeMap::new();477478 let mut input = vec![];479 io::stdin().read_to_end(&mut input)?;480481 if !input.is_empty() {482 let encrypted = encrypt_secret_data(recipients, input)483 .ok_or_else(|| anyhow!("no recipients provided"))?;484 parts.insert(part_name, FleetSecretPart { raw: encrypted });485 }486487 if let Some(public) = parse_public(public, public_file).await? {488 parts.insert(public_name, FleetSecretPart { raw: public });489 }490491 config.replace_shared(492 name,493 FleetSharedSecret {494 owners: machines,495 secret: FleetSecret {496 created_at: Utc::now(),497 expires_at,498 parts,499 },500 },501 );502 }503 Secret::Add {504 machine,505 name,506 replace,507 merge,508 public,509 public_part: public_name,510 public_file,511 part: part_name,512 } => {513 if config.has_secret(&machine, &name) && !replace && !merge {514 bail!("secret already defined.\nUse --replace to override, or --merge to add new parts to existing secret");515 }516517 let mut out = if merge && !replace {518 config519 .host_secret(&machine, &name)520 .context("failed to read existing secret for --merge")?521 } else {522 FleetSecret {523 created_at: Utc::now(),524 expires_at: None,525 parts: BTreeMap::new(),526 }527 };528529 if let Some(secret) = parse_secret().await? {530 let recipient = config.recipient(&machine).await?;531 let encrypted =532 encrypt_secret_data(vec![recipient], secret).expect("recipient provided");533 if out534 .parts535 .insert(part_name.clone(), FleetSecretPart { raw: encrypted })536 .is_some() && !replace537 {538 bail!("part {part_name:?} is already defined");539 }540 }541542 if let Some(public) = parse_public(public, public_file).await? {543 if out544 .parts545 .insert(public_name.clone(), FleetSecretPart { raw: public })546 .is_some() && !replace547 {548 bail!("part {public_name:?} is already defined");549 }550 };551552 config.insert_secret(&machine, name, out);553 }554 #[allow(clippy::await_holding_refcell_ref)]555 Secret::Read {556 name,557 machine,558 part: part_name,559 } => {560 let secret = config.host_secret(&machine, &name)?;561 let Some(secret) = secret.parts.get(&part_name) else {562 bail!("no part {part_name} in secret {name}");563 };564 let data = if secret.raw.encrypted {565 let host = config.host(&machine).await?;566 host.decrypt(secret.raw.clone()).await?567 } else {568 secret.raw.data.clone()569 };570571 stdout().write_all(&data)?;572 }573 Secret::UpdateShared {574 name,575 machine,576 add_machine,577 remove_machine,578 prefer_identities,579 } => {580 // TODO: Forbid updating secrets with set expectedOwners (= not user-managed).581582 let secret = config.shared_secret(&name)?;583 if secret.secret.parts.values().all(|v| !v.raw.encrypted) {584 bail!("no secret");585 }586587 let initial_machines = secret.owners.clone();588 let target_machines = parse_machines(589 initial_machines.clone(),590 machine,591 add_machine,592 remove_machine,593 )?;594595 if target_machines.is_empty() {596 info!("no machines left for secret, removing it");597 config.remove_shared(&name);598 return Ok(());599 }600601 let config_field = &config.config_unchecked_field;602 let field = nix_go!(config_field.sharedSecrets[{ name }]);603604 let updated = update_owner_set(605 &name,606 config,607 secret,608 field,609 &target_machines,610 &prefer_identities,611 )612 .await?;613 config.replace_shared(name, updated);614 }615 Secret::Regenerate { prefer_identities } => {616 info!("checking for secrets to regenerate");617 {618 let _span = info_span!("shared").entered();619 let expected_shared_set = config620 .list_configured_shared()621 .await?622 .into_iter()623 .collect::<HashSet<_>>();624 let shared_set = config.list_shared().into_iter().collect::<HashSet<_>>();625 for missing in expected_shared_set.difference(&shared_set) {626 let config_field = &config.config_unchecked_field;627 let secret = nix_go!(config_field.sharedSecrets[{ missing }]);628 let expected_owners: Option<Vec<String>> =629 nix_go_json!(secret.expectedOwners);630 let Some(expected_owners) = expected_owners else {631 // TODO: Might still need to regenerate632 continue;633 };634 info!("generating secret: {missing}");635 let shared = generate_shared(config, missing, secret, expected_owners)636 .in_current_span()637 .await?;638 config.replace_shared(missing.to_string(), shared)639 }640 }641 for host in config.list_hosts().await? {642 if config.should_skip(&host).await? {643 continue;644 }645646 let _span = info_span!("host", host = host.name).entered();647 let expected_set = host648 .list_configured_secrets()649 .in_current_span()650 .await?651 .into_iter()652 .collect::<HashSet<_>>();653 let stored_set = config654 .list_secrets(&host.name)655 .into_iter()656 .collect::<HashSet<_>>();657 for missing in expected_set.difference(&stored_set) {658 info!("generating secret: {missing}");659 let secret = host.secret_field(missing).in_current_span().await?;660 let generated =661 match generate(config, missing, secret, &[host.name.clone()])662 .in_current_span()663 .await664 {665 Ok(v) => v,666 Err(e) => {667 error!("{e:?}");668 continue;669 }670 };671 config.insert_secret(&host.name, missing.to_string(), generated)672 }673 }674 let mut to_remove = Vec::new();675 for name in &config.list_shared() {676 info!("updating secret: {name}");677 let data = config.shared_secret(name)?;678 let config_field = &config.config_unchecked_field;679 let expected_owners: Vec<String> =680 nix_go_json!(config_field.sharedSecrets[{ name }].expectedOwners);681 if expected_owners.is_empty() {682 warn!("secret was removed from fleet config: {name}, removing from data");683 to_remove.push(name.to_string());684 continue;685 }686687 let secret = nix_go!(config_field.sharedSecrets[{ name }]);688 config.replace_shared(689 name.to_owned(),690 update_owner_set(691 name,692 config,693 data,694 secret,695 &expected_owners,696 &prefer_identities,697 )698 .await?,699 );700 }701 for k in to_remove {702 config.remove_shared(&k);703 }704 }705 Secret::List {} => {706 let _span = info_span!("loading secrets").entered();707 let configured = config.list_configured_shared().await?;708 #[derive(Tabled)]709 struct SecretDisplay {710 #[tabled(rename = "Name")]711 name: String,712 #[tabled(rename = "Owners")]713 owners: String,714 }715 let mut table = vec![];716 for name in configured.iter().cloned() {717 let config = config.clone();718 let expected_owners = config.shared_secret_expected_owners(&name).await?;719 let data = config.shared_secret(&name)?;720 let owners = data721 .owners722 .iter()723 .map(|o| {724 if expected_owners.contains(o) {725 o.green().to_string()726 } else {727 o.red().to_string()728 }729 })730 .collect::<Vec<_>>();731 table.push(SecretDisplay {732 owners: owners.join(", "),733 name,734 })735 }736 info!("loaded\n{}", Table::new(table).to_string())737 }738 Secret::Edit {739 name,740 machine,741 part,742 add,743 } => {744 let secret = config.host_secret(&machine, &name)?;745 if let Some(data) = secret.parts.get(&part) {746 let host = config.host(&machine).await?;747 let secret = host.decrypt(data.raw.clone()).await?;748 String::from_utf8(secret).context("secret is not utf8")?749 } else if add {750 String::new()751 } else {752 bail!("part {part} not found in secret {name}. Did you mean to `--add` it?");753 };754 }755 }756 Ok(())757 }758}759760async fn edit_temp_file(761 builder: tempfile::Builder<'_, '_>,762 r: Vec<u8>,763 header: &str,764 comment: &str,765) -> Result<(Vec<u8>, Option<String>), anyhow::Error> {766 if !stdin().is_tty() {767 // TODO: Also try to open /dev/tty directly?768 bail!("stdin is not tty, can't open editor");769 }770771 use std::fmt::Write;772 let mut file = builder.tempfile()?;773774 let mut full_header = String::new();775 let mut had = false;776 for line in header.trim_end().lines() {777 had = true;778 writeln!(&mut full_header, "{comment}{line}")?;779 }780 if had {781 writeln!(&mut full_header, "{}", comment.trim_end())?;782 }783 writeln!(784 &mut full_header,785 "{comment}Do not touch this header! It will be removed automatically"786 )?;787788 file.write_all(full_header.as_bytes())?;789 file.write_all(&r)?;790791 let abs_path = file.into_temp_path();792 let editor = std::env::var_os("VISUAL")793 .or_else(|| std::env::var_os("EDITOR"))794 .unwrap_or_else(|| "vi".into());795 let editor_args = shlex::bytes::split(editor.as_encoded_bytes())796 .ok_or_else(|| anyhow!("EDITOR env var has wrong syntax"))?;797 let editor_args = editor_args798 .into_iter()799 .map(|v| {800 // Only ASCII subsequences are replaced801 unsafe { OsString::from_encoded_bytes_unchecked(v) }802 })803 .collect_vec();804 let Some((editor, args)) = editor_args.split_first() else {805 bail!("EDITOR env var has no command");806 };807 let mut command = Command::new(editor);808 command.args(args);809810 let path_arg = abs_path.canonicalize()?;811812 // TODO: Save full state, using tcget/_getmode/_setmode813 let was_raw = terminal::is_raw_mode_enabled()?;814 terminal::enable_raw_mode()?;815816 let status = command.arg(path_arg).status().await;817818 if !was_raw {819 terminal::disable_raw_mode()?;820 }821822 let success = match status {823 Ok(s) => s.success(),824 Err(e) if e.kind() == io::ErrorKind::NotFound => {825 bail!("editor not found")826 }827 Err(e) => bail!("editor spawn error: {e}"),828 };829830 let mut file = std::fs::read(&abs_path).context("read editor output")?;831 let Some(v) = file.strip_prefix(full_header.as_bytes()) else {832 todo!();833 };834 todo!();835836 // Ok((success, abs_path))837}cmds/fleet/src/command.rsdiffbeforeafterboth--- a/cmds/fleet/src/command.rs
+++ b/cmds/fleet/src/command.rs
@@ -9,6 +9,8 @@
use tokio_util::codec::{BytesCodec, FramedRead, LinesCodec};
use tracing::debug;
+use crate::host::EscalationStrategy;
+
fn escape_bash(input: &str, out: &mut String) {
const TO_ESCAPE: &str = "$ !\"#&'()*,;<>?[\\]^`{|}";
if input.chars().all(|c| !TO_ESCAPE.contains(c)) {
@@ -27,32 +29,51 @@
fn ostoutf8(os: impl AsRef<OsStr>) -> String {
os.as_ref().to_str().expect("non-utf8 data").to_owned()
}
-#[derive(Clone)]
+
+#[derive(Clone, Debug)]
pub struct MyCommand {
command: String,
args: Vec<String>,
env: Vec<(String, String)>,
ssh_session: Option<Arc<Session>>,
+ escalation: EscalationStrategy,
+ escalate: bool,
}
impl MyCommand {
- pub fn new_on(cmd: impl AsRef<OsStr>, session: Arc<Session>) -> Self {
+ pub fn new_on(
+ escalation: EscalationStrategy,
+ cmd: impl AsRef<OsStr>,
+ session: Arc<Session>,
+ ) -> Self {
assert!(!cmd.as_ref().is_empty());
Self {
command: ostoutf8(cmd),
args: vec![],
env: vec![],
ssh_session: Some(session),
+ escalation,
+ escalate: false,
}
}
- pub fn new(cmd: impl AsRef<OsStr>) -> Self {
+ pub fn new(escalation: EscalationStrategy, cmd: impl AsRef<OsStr>) -> Self {
assert!(!cmd.as_ref().is_empty());
Self {
command: ostoutf8(cmd),
args: vec![],
env: vec![],
ssh_session: None,
+ escalation,
+ escalate: false,
}
}
+ fn new_here(&self, cmd: impl AsRef<OsStr>) -> Self {
+ if let Some(ssh_session) = self.ssh_session.clone() {
+ Self::new_on(self.escalation, cmd, ssh_session)
+ } else {
+ Self::new(self.escalation, cmd)
+ }
+ }
+
fn into_args(self) -> Vec<String> {
let mut out = Vec::new();
if !self.env.is_empty() {
@@ -76,8 +97,7 @@
if self.env.is_empty() {
return self;
}
- let mut out = Self::new("env");
- out.ssh_session = self.ssh_session;
+ let mut out = self.new_here("env");
for (k, v) in self.env {
assert!(!k.contains('='));
out.arg(format!("{k}={v}"));
@@ -160,26 +180,46 @@
self
}
pub fn sudo(mut self) -> Self {
- // TODO: Multiple escalation strategies.
- // Maybe escalation should be moved to ConfigHost, to also support cases
- // when there is no sudo on remote machine, but instead we can reconnect
- // as root using ssh?
- if std::env::var_os("NO_SUDO").is_some() {
- let mut out = Self::new("su");
- out.ssh_session = self.ssh_session.take();
- out.arg("-c").arg(self.into_string());
- out
- } else {
- let mut out = Self::new("sudo");
- out.ssh_session = self.ssh_session.take();
- out.args(self.into_args());
- out
+ self.escalate = true;
+ self
+ }
+ fn wrap_sudo_if_needed(self) -> Self {
+ if !self.escalate {
+ return self;
+ }
+ match self.escalation {
+ EscalationStrategy::Su => {
+ let mut out = self.new_here("su");
+ out.arg("-c").arg(self.into_string());
+ out
+ }
+ EscalationStrategy::Sudo => {
+ let mut out = self.new_here("sudo");
+ out.args(self.into_args());
+ out
+ }
+ EscalationStrategy::Run0 => {
+ // run0 wants interactive authentication by default.
+ let mut run0 = self.new_here("run0");
+ let mut out = self.new_here("script");
+
+ // Red backgrounds messes with fleet formatting
+ run0.arg("--background=");
+ run0.args(self.into_args());
+
+ out.arg("-q");
+ out.arg("/dev/null");
+ out.arg("-c");
+ out.arg(run0.into_string());
+ dbg!(&out);
+ out
+ }
}
}
pub async fn run(self) -> Result<()> {
let str = self.clone().into_string();
- let cmd = self.into_command_new()?;
+ let cmd = self.wrap_sudo_if_needed().into_command_new()?;
match cmd {
Either::Left(cmd) => run_nix_inner(str, cmd, &mut PlainHandler).await?,
Either::Right(cmd) => run_nix_inner_ssh(str, cmd, &mut PlainHandler).await?,
@@ -192,7 +232,7 @@
}
pub async fn run_bytes(self) -> Result<Vec<u8>> {
let str = self.clone().into_string();
- let cmd = self.into_command_new()?;
+ let cmd = self.wrap_sudo_if_needed().into_command_new()?;
let v = match cmd {
Either::Left(cmd) => run_nix_inner_stdout(str, cmd, &mut PlainHandler).await?,
Either::Right(cmd) => run_nix_inner_stdout_ssh(str, cmd, &mut PlainHandler).await?,
@@ -200,17 +240,17 @@
Ok(v)
}
- pub async fn run_nix_string(self) -> Result<String> {
+ pub async fn run_nix_string(mut self) -> Result<String> {
let str = self.clone().into_string();
- let mut cmd = self.into_command();
- cmd.arg("--log-format").arg("internal-json");
+ self.arg("--log-format").arg("internal-json");
+ let mut cmd = self.wrap_sudo_if_needed().into_command();
let bytes = run_nix_inner_stdout(str, cmd, &mut NixHandler::default()).await?;
Ok(String::from_utf8(bytes)?)
}
- pub async fn run_nix(self) -> Result<()> {
+ pub async fn run_nix(mut self) -> Result<()> {
let str = self.clone().into_string();
- let mut cmd = self.into_command();
- cmd.arg("--log-format").arg("internal-json");
+ self.arg("--log-format").arg("internal-json");
+ let mut cmd = self.wrap_sudo_if_needed().into_command();
cmd.stdout(Stdio::inherit());
run_nix_inner(str, cmd, &mut NixHandler::default()).await
}
cmds/fleet/src/host.rsdiffbeforeafterboth--- a/cmds/fleet/src/host.rs
+++ b/cmds/fleet/src/host.rs
@@ -1,5 +1,5 @@
use std::{
- cell::OnceCell,
+ cell::{LazyCell, OnceCell},
collections::BTreeMap,
env::current_dir,
ffi::{OsStr, OsString},
@@ -14,7 +14,7 @@
use anyhow::{anyhow, bail, ensure, Context, Result};
use clap::Parser;
use fleet_shared::SecretData;
-use nix_eval::{nix_go, nix_go_json, NixSessionPool, Value};
+use nix_eval::{nix_go, nix_go_json, util::assert_warn, NixSessionPool, Value};
use nom::{
bytes::complete::take_while1,
character::complete::char,
@@ -25,6 +25,7 @@
use openssh::SessionBuilder;
use serde::de::DeserializeOwned;
use tempfile::NamedTempFile;
+use tracing::error;
use crate::{
command::MyCommand,
@@ -39,8 +40,6 @@
pub nix_args: Vec<OsString>,
/// fleet_config.config
pub config_field: Value,
- /// fleet_config.unchecked.config
- pub config_unchecked_field: Value,
/// import nixpkgs {system = local};
pub default_pkgs: Value,
@@ -57,6 +56,13 @@
}
}
+#[derive(Clone, Copy, Debug)]
+pub enum EscalationStrategy {
+ Sudo,
+ Run0,
+ Su,
+}
+
pub struct ConfigHost {
config: Config,
pub name: String,
@@ -64,32 +70,57 @@
pub session: OnceLock<Arc<openssh::Session>>,
groups: OnceCell<Vec<String>>,
- pub nixos_config: Option<Value>,
+ pub host_config: Option<Value>,
+ pub nixos_config: OnceCell<Value>,
}
impl ConfigHost {
+ pub async fn escalation_strategy(&self) -> Result<EscalationStrategy> {
+ // Prefer sudo, as run0 has some gotchas with polkit
+ // and too many repeating prompts.
+ if let Ok(_) = self.find_in_path("sudo").await {
+ return Ok(EscalationStrategy::Sudo);
+ }
+ if let Ok(_) = self.find_in_path("run0").await {
+ return Ok(EscalationStrategy::Run0);
+ }
+ Ok(EscalationStrategy::Su)
+ }
+ // TOCTOU is possible here in case if config is changed, but this case is not handled anywhere anyway,
+ // assuming getting tags always returns the same value.
pub async fn tags(&self) -> Result<Vec<String>> {
if let Some(v) = self.groups.get() {
return Ok(v.clone());
}
- // TOCTOU is possible here in case if config is changed, but this case is not handled anywhere anyway,
- // assuming getting tags always returns the same value.
- let Some(nixos_config) = &self.nixos_config else {
+ let Some(host_config) = &self.host_config else {
return Ok(vec![]);
};
- let tags: Vec<String> = nix_go_json!(nixos_config.tags);
+ let tags: Vec<String> = nix_go_json!(host_config.tags);
let _ = self.groups.set(tags.clone());
Ok(tags)
}
+ pub async fn nixos_config(&self) -> Result<Value> {
+ if let Some(v) = self.nixos_config.get() {
+ return Ok(v.clone());
+ }
+ let Some(host_config) = &self.host_config else {
+ bail!("local host has no nixos_config");
+ };
+ let nixos_config = nix_go!(host_config.nixos.config);
+ assert_warn("nixos config evaluation", &nixos_config).await?;
+
+ let _ = self.nixos_config.set(nixos_config.clone());
+
+ Ok(nixos_config)
+ }
async fn open_session(&self) -> Result<Arc<openssh::Session>> {
assert!(!self.local, "do not open ssh connection to local session");
// FIXME: TOCTOU
if let Some(session) = &self.session.get() {
return Ok((*session).clone());
};
- let session = SessionBuilder::default();
-
+ let mut session = SessionBuilder::default();
let session = session
.connect(&self.name)
.await
@@ -129,6 +160,34 @@
let text = self.read_file_text(path).await?;
Ok(serde_json::from_str(&text)?)
}
+ pub async fn read_env(&self, env: &str) -> Result<String> {
+ let mut cmd = self.cmd("printenv").await?;
+ cmd.arg(env);
+ Ok(cmd.run_string().await?)
+ }
+ pub async fn find_in_path(&self, command: &str) -> Result<String> {
+ // // `which` is not a part of coreutils, and it might not exist on machine.
+ // let path = self.read_env("PATH").await?;
+ // // Assuming delimiter is :, we don't work with windows host, this check will be much
+ // // more sophisticated in remowt backend (and quicker, since actual PATH search will be done on remote machine)
+ // for ele in path.split(':') {
+ // let test_path = format!("{ele}/{cmd}");
+ // test -x etc
+ // }
+ // let mut cmd = self.cmd("printenv").await?;
+ // cmd.arg(env);
+ // Ok(cmd.run_string().await?)
+ // Assuming this is an environment issue if which doesn't exist, will be fixed with remowt.
+ let mut cmd = self
+ .cmd_escalation(
+ // Not used
+ EscalationStrategy::Su,
+ "which",
+ )
+ .await?;
+ cmd.arg(command);
+ cmd.run_string().await
+ }
pub async fn read_file_value<D: FromStr>(&self, path: impl AsRef<OsStr>) -> Result<D>
where
<D as FromStr>::Err: Display,
@@ -137,11 +196,19 @@
D::from_str(&text).map_err(|e| anyhow!("failed to parse value: {e}"))
}
pub async fn cmd(&self, cmd: impl AsRef<OsStr>) -> Result<MyCommand> {
+ self.cmd_escalation(self.escalation_strategy().await?, cmd)
+ .await
+ }
+ pub async fn cmd_escalation(
+ &self,
+ escalation: EscalationStrategy,
+ cmd: impl AsRef<OsStr>,
+ ) -> Result<MyCommand> {
if self.local {
- Ok(MyCommand::new(cmd))
+ Ok(MyCommand::new(escalation, cmd))
} else {
let session = self.open_session().await?;
- Ok(MyCommand::new_on(cmd, session))
+ Ok(MyCommand::new_on(escalation, cmd, session))
}
}
@@ -181,7 +248,11 @@
// Path is located locally, thus already trusted.
return Ok(path.to_owned());
}
- let mut nix = MyCommand::new("nix");
+ let mut nix = MyCommand::new(
+ // Not used
+ EscalationStrategy::Su,
+ "nix",
+ );
nix.arg("copy")
.arg("--substitute-on-destination")
.comparg("--to", format!("ssh-ng://{}", self.name))
@@ -210,9 +281,7 @@
}
pub async fn list_configured_secrets(&self) -> Result<Vec<String>> {
- let Some(nixos) = &self.nixos_config else {
- return Ok(vec![]);
- };
+ let nixos = self.nixos_config().await?;
let secrets = nix_go!(nixos.secrets);
let mut out = Vec::new();
for name in secrets.list_fields().await? {
@@ -226,18 +295,14 @@
Ok(out)
}
pub async fn secret_field(&self, name: &str) -> Result<Value> {
- let Some(nixos) = &self.nixos_config else {
- bail!("host is virtual and has no secrets");
- };
+ let nixos = self.nixos_config().await?;
Ok(nix_go!(nixos.secrets[{ name }]))
}
/// Packages for this host, resolved with nixpkgs overlays
pub async fn pkgs(&self) -> Result<Value> {
- let Some(nixos) = &self.nixos_config else {
- return Ok(self.config.default_pkgs.clone());
- };
- Ok(nix_go!(nixos.nixpkgs.resolvedPkgs))
+ let nixos = self.nixos_config().await?;
+ Ok(nix_go!(nixos._resolvedPkgs))
}
}
@@ -317,7 +382,8 @@
name: "<virtual localhost>".to_owned(),
local: true,
session: OnceLock::new(),
- nixos_config: None,
+ host_config: None,
+ nixos_config: OnceCell::new(),
groups: {
let cell = OnceCell::new();
let _ = cell.set(vec![]);
@@ -327,19 +393,22 @@
}
pub async fn host(&self, name: &str) -> Result<ConfigHost> {
- let config = &self.config_unchecked_field;
- let nixos_config = nix_go!(config.hosts[{ name }].nixosSystem.config);
+ let config = &self.config_field;
+ let host_config = nix_go!(config.hosts[{ name }]);
+
+
Ok(ConfigHost {
config: self.clone(),
name: name.to_owned(),
local: self.is_local(name),
session: OnceLock::new(),
- nixos_config: Some(nixos_config),
+ host_config: Some(host_config),
+ nixos_config: OnceCell::new(),
groups: OnceCell::new(),
})
}
pub async fn list_hosts(&self) -> Result<Vec<ConfigHost>> {
- let config = &self.config_unchecked_field;
+ let config = &self.config_field;
let names = nix_go!(config.hosts).list_fields().await?;
let mut out = vec![];
for name in names {
@@ -348,8 +417,8 @@
Ok(out)
}
pub async fn system_config(&self, host: &str) -> Result<Value> {
- let fleet_field = &self.config_unchecked_field;
- Ok(nix_go!(fleet_field.hosts[{ host }].nixosSystem.config))
+ let fleet_field = &self.config_field;
+ Ok(nix_go!(fleet_field.hosts[{ host }].nixos.config))
}
pub(super) fn data(&self) -> MutexGuard<FleetData> {
@@ -360,7 +429,7 @@
}
/// Shared secrets configured in fleet.nix or in flake
pub async fn list_configured_shared(&self) -> Result<Vec<String>> {
- let config_field = &self.config_unchecked_field;
+ let config_field = &self.config_field;
Ok(nix_go!(config_field.sharedSecrets).list_fields().await?)
}
/// Shared secrets configured in fleet.nix
@@ -420,7 +489,7 @@
Ok(secret.clone())
}
pub async fn shared_secret_expected_owners(&self, secret: &str) -> Result<Vec<String>> {
- let config_field = &self.config_unchecked_field;
+ let config_field = &self.config_field;
Ok(nix_go_json!(
config_field.sharedSecrets[{ secret }].expectedOwners
))
@@ -525,26 +594,27 @@
}
let local_system = self.local_system.clone();
+ let mut fleet_data_path = directory.clone();
+ fleet_data_path.push("fleet.nix");
+ let bytes = std::fs::read_to_string(fleet_data_path)?;
+ let data: Mutex<FleetData> = nixlike::parse_str(&bytes)?;
+
let fleet_root = Value::binding(root_field, "fleetConfigurations").await?;
- let fleet_field = nix_go!(fleet_root.default);
+ let fleet_field = nix_go!(fleet_root.default({ data }));
let config_field = nix_go!(fleet_field.config);
- let config_unchecked_field = nix_go!(fleet_field.unchecked.config);
+ assert_warn("fleet config evaluation", &config_field).await?;
+
let import = nix_go!(builtins_field.import);
- let overlays = nix_go!(config_unchecked_field.overlays);
- let nixpkgs = nix_go!(fleet_field.nixpkgs | import);
+ let overlays = nix_go!(config_field.nixpkgs.overlays);
+ let nixpkgs = nix_go!(fleet_field.nixpkgs.buildUsing | import);
let default_pkgs = nix_go!(nixpkgs(Obj {
overlays,
system: { self.local_system.clone() },
}));
- let mut fleet_data_path = directory.clone();
- fleet_data_path.push("fleet.nix");
- let bytes = std::fs::read_to_string(fleet_data_path)?;
- let data = nixlike::parse_str(&bytes)?;
-
Ok(Config(Arc::new(FleetConfigInternals {
opts: self,
directory,
@@ -552,7 +622,6 @@
local_system,
nix_args,
config_field,
- config_unchecked_field,
default_pkgs,
})))
}
cmds/fleet/src/main.rsdiffbeforeafterboth--- a/cmds/fleet/src/main.rs
+++ b/cmds/fleet/src/main.rs
@@ -58,7 +58,7 @@
path.push("file://");
path.push(entry.path());
- let mut status = MyCommand::new("nix");
+ let mut status = config.local_host().cmd("nix").await?;
status.args(&config.nix_args);
status.arg("store").arg("prefetch-file").arg(path);
status.run_nix_string().instrument(span).await?;
crates/nix-eval/Cargo.tomldiffbeforeafterboth--- a/crates/nix-eval/Cargo.toml
+++ b/crates/nix-eval/Cargo.toml
@@ -5,6 +5,7 @@
build = "build.rs"
[dependencies]
+anyhow.workspace = true
better-command.workspace = true
futures = "0.3.30"
itertools = "0.13.0"
crates/nix-eval/src/lib.rsdiffbeforeafterboth--- a/crates/nix-eval/src/lib.rs
+++ b/crates/nix-eval/src/lib.rs
@@ -17,6 +17,7 @@
// Contains macros helpers
#[doc(hidden)]
pub mod macros;
+pub mod util;
// #[allow(non_upper_case_globals, non_camel_case_types, non_snake_case)]
// mod nix_raw {
// include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
crates/nix-eval/src/util.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/nix-eval/src/util.rs
@@ -0,0 +1,30 @@
+use anyhow::bail;
+use tracing::{debug, warn};
+use std::time::Instant;
+
+use crate::{nix_go_json, Value};
+
+pub async fn assert_warn(action: &str, val: &Value) -> anyhow::Result<()> {
+ let before_errors = Instant::now();
+ let errors: Vec<String> = nix_go_json!(val.errors);
+ debug!("errors evaluation took {:?}", before_errors.elapsed());
+ if !errors.is_empty() {
+ bail!(
+ "{action} failed with error{}{}",
+ (errors.len() != 1).then_some("s:\n- ").unwrap_or(": "),
+ errors.join("\n- "),
+ );
+ }
+
+ let before_errors = Instant::now();
+ let warnings: Vec<String> = nix_go_json!(val.warnings);
+ debug!("warnings evaluation took {:?}", before_errors.elapsed());
+ if !warnings.is_empty() {
+ warn!(
+ "{action} completed with warning{}{}",
+ (warnings.len() != 1).then_some("s:\n- ").unwrap_or(": "),
+ warnings.join("\n- "),
+ );
+ }
+ Ok(())
+}
crates/nix-eval/src/value.rsdiffbeforeafterboth--- a/crates/nix-eval/src/value.rs
+++ b/crates/nix-eval/src/value.rs
@@ -44,14 +44,14 @@
let v = nixlike::format_identifier(k.as_str());
write!(f, ".{v}")
}
- Index::Apply(o) => {
- write!(f, "<apply>({o})")
+ Index::Apply(_) => {
+ write!(f, "<apply>(...)")
}
Index::Expr(e) => {
write!(f, "[{}]", e.out)
}
- Index::ExprApply(e) => {
- write!(f, "<apply>({})", e.out)
+ Index::ExprApply(_) => {
+ write!(f, "<apply>(...)")
}
Index::Pipe(e) => {
write!(f, "<map>({})", e.out)
flake.nixdiffbeforeafterboth--- a/flake.nix
+++ b/flake.nix
@@ -25,18 +25,23 @@
flake-parts.lib.mkFlake {
inherit inputs;
} {
- flake = let
- inherit (inputs.nixpkgs.lib) mapAttrs;
- in {
- lib = import ./lib {
- fleetPkgsForPkgs = pkgs:
- import ./pkgs {
- inherit (pkgs) callPackage;
- craneLib = crane.mkLib pkgs;
- };
- };
+ flake = rec {
+ lib =
+ (import ./lib {
+ inherit (inputs.nixpkgs) lib;
+ })
+ // {
+ fleetConfiguration = throw "function-based interface is deprecated, use flake-parts syntax instead";
+ };
+ flakeModules.default = (import ./lib/flakePart.nix {
+ inherit crane;
+ });
+ flakeModule = flakeModules.default;
+
# To be used with https://github.com/NixOS/nix/pull/8892
- schemas = {
+ schemas = let
+ inherit (inputs.nixpkgs.lib) mapAttrs;
+ in {
fleetConfigurations = {
version = 1;
doc = ''
@@ -69,7 +74,8 @@
pkgs,
...
}: let
- inherit (lib) mapAttrs' elem;
+ inherit (lib.attrSets) mapAttrs';
+ inherit (lib.lists) elem;
# Can also be built for darwin, through it is not usual to deploy nixos systems from macos machines.
# I have no hardware for such testing, thus only adding machines I actually have and use.
#
@@ -108,6 +114,7 @@
pkg-config
openssl
bacon
+ nil
];
};
};
lib/default.nixdiffbeforeafterboth--- a/lib/default.nix
+++ b/lib/default.nix
@@ -1,60 +1,121 @@
-{fleetPkgsForPkgs}: {
- fleetConfiguration = {
- # TODO: Provide by fleet, instead of requesting user to provide it.
- # This is not good that user needs to provide it, as it becomes a flake data, and fleet arbitrarily rewriting it
- # always dirnets the flake. Instead, fleetConfiguration should return function, parameters of which should be filled
- # by fleet itself, which is possible since fleet moving to nix repl execution.
- data,
- nixpkgs,
- overlays ? [],
- hosts,
- fleetModules,
- nixosModules ? [],
- extraFleetLib ? {},
- }: let
- hostNames = nixpkgs.lib.attrNames hosts;
- fleetLib =
- (import ./fleetLib.nix {
- inherit nixpkgs hostNames;
- })
- // extraFleetLib;
- in let
- root = nixpkgs.lib.evalModules {
- modules =
- (import ../modules/fleet/_modules.nix)
- ++ [
- data
- ({...}: {
- inherit nixosModules hosts;
- overlays = [(final: prev: (fleetPkgsForPkgs final))] ++ overlays;
- })
- ]
- ++ fleetModules;
- specialArgs = {
- inherit nixpkgs fleetLib;
+# Shared functions for fleet configuration, available as `fleet` module argument
+{lib}: let
+ inherit (lib.trivial) isFunction;
+ inherit (lib.options) mkOption mergeOneOption;
+ inherit (lib.modules) mkOverride;
+ inherit (lib.types) listOf submodule attrsOf mkOptionType;
+ inherit (lib.strings) optionalString;
+in rec {
+ types = {
+ overlay = mkOptionType {
+ name = "nixpkgs-overlay";
+ description = "nixpkgs overlay";
+ check = isFunction;
+ merge = mergeOneOption;
+ };
+ listOfOverlay = listOf types.overlay;
+
+ mkHostsType = module: attrsOf (submodule module);
+ };
+
+ options = {
+ mkHostsOption = module:
+ mkOption {
+ type = types.mkHostsType module;
};
- };
- failedAssertions = map (x: x.message) (nixpkgs.lib.filter (x: !x.assertion) root.config.assertions);
- checkedRoot =
- if failedAssertions != []
- then throw "Fleet failed assertions:\n${nixpkgs.lib.concatStringsSep "\n" (map (x: "- ${x}") failedAssertions)}"
- else nixpkgs.lib.showWarnings root.config.warnings root;
- withData = {
- root,
- data,
- }: {
- config = root.config;
- };
- defaultData = withData {
- inherit data;
- root = checkedRoot;
- };
- uncheckedData = withData {inherit data root;};
- in {
- inherit nixpkgs overlays;
- inherit (defaultData) config;
- unchecked = {
- inherit (uncheckedData) config;
- };
};
+
+ inherit (options) mkHostsOption;
+
+ modules = {
+ # mkDefault = mkOverride 1000
+ # For places, where fleet knows better than nixpkgs defaults.
+ mkFleetDefault = mkOverride 999;
+ # Some generators use mkDefault, but optionDefault is set by nixpkgs.
+ mkFleetGeneratorDefault = mkOverride 1001;
+ };
+
+ inherit (modules) mkFleetDefault mkFleetGeneratorDefault;
+
+ secrets = {
+ mkPassword = {size ? 32}: {
+ coreutils,
+ mkSecretGenerator,
+ ...
+ }:
+ mkSecretGenerator {
+ script = ''
+ mkdir $out
+ gh generate password -o $out/secret --size ${toString size}
+ '';
+ };
+
+ mkEd25519 = {
+ noEmbedPublic ? false,
+ encoding ? null,
+ }: {mkSecretGenerator, ...}:
+ mkSecretGenerator {
+ script = ''
+ mkdir $out
+ gh generate ed25519 -p $out/public -s $out/secret \
+ ${optionalString noEmbedPublic "--no-embed-public"} \
+ ${optionalString (encoding != null) "--encoding=${encoding}"}
+ '';
+ };
+
+ mkX25519 = {encoding ? null}: {mkSecretGenerator, ...}:
+ mkSecretGenerator {
+ script = ''
+ mkdir $out
+ gh generate x25519 -p $out/public -s $out/secret \
+ ${optionalString (encoding != null) "--encoding=${encoding}"}
+ '';
+ };
+
+ mkRsa = {size ? 4096}: {
+ openssl,
+ mkSecretGenerator,
+ ...
+ }:
+ mkSecretGenerator {
+ script = ''
+ mkdir $out
+
+ ${openssl}/bin/openssl genrsa -out rsa_private.key ${toString size}
+ ${openssl}/bin/openssl rsa -in rsa_private.key -pubout -out rsa_public.key
+
+ cat rsa_private.key | gh private -o $out/secret
+ cat rsa_public.key | gh public -o $out/public
+ '';
+ };
+
+ mkBytes = {
+ count ? 32,
+ encoding,
+ noNuls ? false,
+ }: {mkSecretGenerator, ...}:
+ mkSecretGenerator {
+ script = ''
+ mkdir $out
+ gh generate bytes --count=${toString count} --encoding=${encoding} -o $out/secret \
+ ${optionalString noNuls "--no-nuls"}
+ '';
+ };
+ mkHexBytes = {count ? 32}:
+ mkBytes {
+ inherit count;
+ encoding = "hex";
+ };
+ mkBase64Bytes = {count ? 32}:
+ mkBytes {
+ inherit count;
+ encoding = "base64";
+ };
+
+ # Wireguard
+ # mkWireguard = {}: mkX25519 {encoding = "base64";};
+ # mkWireguardPsk = {}: mkBase64Bytes {count = 32;};
+ };
+
+ inherit (secrets) mkPassword mkEd25519 mkX25519 mkRsa mkBytes mkHexBytes mkBase64Bytes;
}
lib/flakePart.nixdiffbeforeafterboth--- /dev/null
+++ b/lib/flakePart.nix
@@ -0,0 +1,70 @@
+{crane}: {
+ fleetLib,
+ lib,
+ config,
+ ...
+}: let
+ inherit (lib.options) mkOption;
+ inherit (lib.attrsets) mapAttrs;
+ inherit (lib.types) lazyAttrsOf deferredModule unspecified;
+ inherit (fleetLib.options) mkHostsOption;
+in {
+ options.fleetModules = mkOption {
+ type = lazyAttrsOf unspecified;
+ default = {};
+ };
+ options.fleetConfigurations = mkOption {
+ type = lazyAttrsOf deferredModule;
+ apply = nameToModule:
+ mapAttrs (
+ name: module: data: let
+ # To use user-provided nixpkgs, we first need to extract wanted nixpkgs attribute,
+ # to do that, evaluate all the modules with only needed option declared.
+ bootstrapEval = lib.evalModules {
+ modules = [
+ module
+ {
+ options.nixpkgs.buildUsing = mkOption {
+ description = ''
+ Nixpkgs to use for fleetConfiguration evaluation.
+ '';
+ };
+ config._module.check = false;
+ }
+ ];
+ };
+ bootstrapNixpkgs = bootstrapEval.config.nixpkgs.buildUsing;
+ normalEval = bootstrapNixpkgs.lib.evalModules {
+ modules =
+ (import ../modules/fleet/_modules.nix)
+ ++ [
+ data
+ module
+ {
+ options.hosts = mkHostsOption {
+ nixos.nixpkgs.overlays = [
+ (final: prev: {
+ # FIXME: make this name not conflicting
+ craneLib = crane.mkLib prev;
+ })
+ ];
+ };
+ }
+ ];
+ specialArgs.fleetLib = import ../lib {
+ inherit (bootstrapNixpkgs) lib;
+ };
+ };
+ in
+ normalEval
+ )
+ nameToModule;
+ };
+ config = {
+ _module.args.fleetLib = import ../lib {inherit lib;};
+ flake.fleetConfigurations = config.fleetConfigurations;
+ flake.fleetModules = config.fleetModules;
+ };
+
+ _file = ./flakePart.nix;
+}
lib/fleetLib.nixdiffbeforeafterboth--- a/lib/fleetLib.nix
+++ /dev/null
@@ -1,144 +0,0 @@
-# Shared functions for fleet configuration, available as `fleet` module argument
-{
- nixpkgs,
- hostNames,
-}: let
- inherit (nixpkgs) lib;
- inherit (lib) listToAttrs remove unique crossLists sort elemAt mkOptionType mkOverride optionalString;
- inherit (lib.types) listOf coercedTo oneOf submodule;
-in rec {
- hostsToAttrs = f:
- listToAttrs (
- map (name: {
- inherit name;
- value = f name;
- })
- hostNames
- );
- hostsCartesian = remove null (
- unique (
- crossLists
- (
- a: b:
- if a == b
- then null
- else hostsPair a b
- ) [hostNames hostNames]
- )
- );
- hostsPair = this: other: let
- sorted = sort (a: b: a < b) [this other];
- in {
- a = elemAt sorted 0;
- b = elemAt sorted 1;
- };
- hostPairName = this: other:
- if this < other
- then "${this}-${other}"
- else "${other}-${this}";
-
- types = rec {
- anyModule = mkOptionType {
- name = "submodule";
- inherit (submodule {}) check;
- merge = lib.options.mergeOneOption;
- description = "Nixos module";
- };
- listOfAnyModuleStrict =
- listOf anyModule;
- listOfAnyModule =
- coercedTo (oneOf [listOfAnyModuleStrict anyModule]) (
- v:
- if builtins.isAttrs v
- then [v]
- else if builtins.isFunction v
- then [v]
- else v
- )
- listOfAnyModuleStrict;
- };
-
- # mkDefault = mkOverride 1000
- # For places, where fleet knows better than nixpkgs defaults.
- mkFleetDefault = mkOverride 999;
- # Some generators use mkDefault, but optionDefault is set by nixpkgs.
- mkFleetGeneratorDefault = mkOverride 1001;
-
- mkPassword = {size ? 32}: {
- coreutils,
- mkSecretGenerator,
- ...
- }:
- mkSecretGenerator {
- script = ''
- mkdir $out
- gh generate password -o $out/secret --size ${toString size}
- '';
- };
-
- mkEd25519 = {
- noEmbedPublic ? false,
- encoding ? null,
- }: {mkSecretGenerator, ...}:
- mkSecretGenerator {
- script = ''
- mkdir $out
- gh generate ed25519 -p $out/public -s $out/secret \
- ${optionalString noEmbedPublic "--no-embed-public"} \
- ${optionalString (encoding != null) "--encoding=${encoding}"}
- '';
- };
-
- mkX25519 = {encoding ? null}: {mkSecretGenerator, ...}:
- mkSecretGenerator {
- script = ''
- mkdir $out
- gh generate x25519 -p $out/public -s $out/secret \
- ${optionalString (encoding != null) "--encoding=${encoding}"}
- '';
- };
-
- mkRsa = {size ? 4096}: {
- openssl,
- mkSecretGenerator,
- ...
- }:
- mkSecretGenerator {
- script = ''
- mkdir $out
-
- ${openssl}/bin/openssl genrsa -out rsa_private.key ${toString size}
- ${openssl}/bin/openssl rsa -in rsa_private.key -pubout -out rsa_public.key
-
- cat rsa_private.key | gh private -o $out/secret
- cat rsa_public.key | gh public -o $out/public
- '';
- };
-
- mkBytes = {
- count ? 32,
- encoding,
- noNuls ? false,
- }: {mkSecretGenerator, ...}:
- mkSecretGenerator {
- script = ''
- mkdir $out
- gh generate bytes --count=${toString count} --encoding=${encoding} -o $out/secret \
- ${optionalString noNuls "--no-nuls"}
- '';
- };
- mkHexBytes = {count ? 32}:
- mkBytes {
- inherit count;
- encoding = "hex";
- };
- mkBase64Bytes = {count ? 32}:
- mkBytes {
- inherit count;
- encoding = "base64";
- };
-
- # Wireguard
- # mkWireguard = {}: mkX25519 {encoding = "base64";};
- # mkWireguardPsk = {}: mkBase64Bytes {count = 32;};
-}
modules/fleet/_modules.nixdiffbeforeafterboth--- a/modules/fleet/_modules.nix
+++ b/modules/fleet/_modules.nix
@@ -1,5 +1,9 @@
[
./assertions.nix
+ ./fleetLib.nix
+ ./hosts.nix
./meta.nix
+ ./nixos.nix
+ ./nixpkgs.nix
./secrets.nix
]
modules/fleet/assertions.nixdiffbeforeafterboth--- a/modules/fleet/assertions.nix
+++ b/modules/fleet/assertions.nix
@@ -1,6 +1,11 @@
-{lib, ...}: let
- inherit (lib) mkOption;
+{
+ lib,
+ config,
+ ...
+}: let
+ inherit (lib.options) mkOption;
inherit (lib.types) listOf unspecified str;
+ inherit (lib.lists) map filter;
in {
options = {
assertions = mkOption {
@@ -30,6 +35,15 @@
the evaluation of the system configuration.
'';
};
+ errors = mkOption {
+ type = listOf str;
+ internal = true;
+ description = ''
+ Similar to warnings, however build will fail if any error exists.
+ '';
+ };
};
- # impl of assertions is in <fleet/lib/default.nix>
+ config.errors =
+ map (v: v.message)
+ (filter (v: !v.assertion) config.assertions);
}
modules/fleet/fleetLib.nixdiffbeforeafterboth--- /dev/null
+++ b/modules/fleet/fleetLib.nix
@@ -0,0 +1,9 @@
+{
+ lib,
+ config,
+ ...
+}: {
+ _module.args.fleetLib = import ../../lib {
+ inherit lib;
+ };
+}
modules/fleet/hosts.nixdiffbeforeafterboth--- /dev/null
+++ b/modules/fleet/hosts.nix
@@ -0,0 +1,40 @@
+{
+ lib,
+ fleetLib,
+ ...
+}: let
+ inherit (fleetLib.modules) mkFleetGeneratorDefault;
+ inherit (fleetLib.types) mkHostsType;
+ inherit (lib.options) mkOption;
+ inherit (lib.types) str listOf;
+in {
+ options = {
+ hosts = mkOption {
+ type = mkHostsType ({config, ...}: {
+ options = {
+ system = mkOption {
+ type = str;
+ description = "Type of the system.";
+ };
+ # TODO: This is part of fleet.nix, move it to separate toplevel data config option.
+ encryptionKey = mkOption {
+ type = str;
+ description = "Rage SSH encryption key for secrets.";
+ };
+ tags = mkOption {
+ type = listOf str;
+ description = "Host tag. In CLI, you can refer to all hosts having this tag using @tag syntax.";
+ };
+ };
+ config = {
+ nixos.networking.hostName = mkFleetGeneratorDefault config._module.args.name;
+ tags = ["all"];
+ };
+ _file = ./meta.nix;
+ });
+ default = {};
+ description = "Configurations of individual hosts";
+ };
+ };
+ _file = ./meta.nix;
+}
modules/fleet/meta.nixdiffbeforeafterboth--- a/modules/fleet/meta.nix
+++ b/modules/fleet/meta.nix
@@ -1,89 +1,8 @@
-{
- lib,
- fleetLib,
- config,
- nixpkgs,
- ...
-}: let
- inherit (fleetLib) hostsToAttrs mkFleetGeneratorDefault;
- inherit (fleetLib.types) listOfAnyModule;
- inherit (lib) mkOption mkOptionType;
- inherit (lib.types) str unspecified attrsOf listOf submodule;
- hostModule = {...} @ hostConfig: let
- hostName = hostConfig.config._module.args.name;
- in {
- options = {
- nixosModules = mkOption {
- # Not too strict, but nixos module system will fix everything.
- type =
- listOfAnyModule;
-
- description = "List of nixos modules";
- default = [];
- };
- system = mkOption {
- type = str;
- description = "Type of system";
- };
- encryptionKey = mkOption {
- type = str;
- description = "Encryption key";
- };
- nixosSystem = mkOption {
- type = unspecified;
- description = "Nixos configuration";
- };
- nixpkgs = mkOption {
- type = unspecified;
- description = "Nixpkgs override";
- default = nixpkgs;
- };
- };
- config = {
- nixosSystem = hostConfig.config.nixpkgs.lib.nixosSystem {
- inherit (hostConfig.config) system;
- modules = hostConfig.config.nixosModules;
- specialArgs = {
- inherit fleetLib;
- fleet = hostsToAttrs (host: config.hosts.${host}.nixosSystem.config);
- };
- };
- nixosModules.networking.hostName = mkFleetGeneratorDefault hostName;
- };
- };
- overlayType = mkOptionType {
- name = "nixpkgs-overlay";
- description = "nixpkgs overlay";
- check = lib.isFunction;
- merge = lib.mergeOneOption;
- };
+{lib, ...}: let
+ inherit (lib.modules) mkRemovedOptionModule;
in {
- options = {
- hosts = mkOption {
- type = attrsOf (submodule hostModule);
- default = {};
- description = "Configurations of individual hosts";
- };
- nixosModules = mkOption {
- type = listOfAnyModule;
- description = "Modules, which should be added to every system";
- default = [];
- };
- overlays = mkOption {
- default = [];
- type = listOf overlayType;
- };
- };
- config = {
- hosts = hostsToAttrs (host: {
- nixosModules =
- config.nixosModules
- ++ [
- {
- nixpkgs.overlays = config.overlays;
- }
- ];
- });
- nixosModules = import ../../nixos/modules/module-list.nix;
- };
+ imports = [
+ (mkRemovedOptionModule ["fleetModules"] "replaced with imports.")
+ (mkRemovedOptionModule ["data"] "data is now provided by fleet itself, you can remove your import.")
+ ];
}
modules/fleet/nixos.nixdiffbeforeafterboth--- /dev/null
+++ b/modules/fleet/nixos.nix
@@ -0,0 +1,55 @@
+{
+ lib,
+ fleetLib,
+ config,
+ ...
+}: let
+ inherit (lib.attrsets) mapAttrs;
+ inherit (lib.options) mkOption;
+ inherit (lib.types) deferredModule deferredModuleWith;
+ inherit (lib.modules) mkRemovedOptionModule;
+ inherit (fleetLib.options) mkHostsOption;
+
+ _file = ./nixos.nix;
+in {
+ options = {
+ nixos = mkOption {
+ description = ''
+ Nixos configuration for all hosts.
+ '';
+ type = deferredModule;
+ };
+ hosts = mkHostsOption (hostArgs: {
+ inherit _file;
+ options = {
+ nixos = mkOption {
+ description = ''
+ Nixos configuration for the current host.
+ '';
+ type = deferredModuleWith {
+ staticModules = import ../../nixos/modules/module-list.nix;
+ };
+ apply = module:
+ config.nixpkgs.buildUsing.lib.nixosSystem {
+ inherit (hostArgs.config) system;
+ modules = [module];
+ };
+ };
+ };
+ config = {
+ # imports = [
+ # (mkRemovedOptionModule ["nixosModules"] "replaced with hosts.*.nixos.imports.")
+ # ];
+ nixos = {
+ imports = [
+ config.nixos
+ ];
+ config._module.args.fleet = mapAttrs (_: value: value.nixos.config) config.hosts;
+ };
+ };
+ });
+ };
+ imports = [
+ (mkRemovedOptionModule ["nixosModules"] "replaced with nixos.imports.")
+ ];
+}
modules/fleet/nixpkgs.nixdiffbeforeafterboth--- /dev/null
+++ b/modules/fleet/nixpkgs.nix
@@ -0,0 +1,58 @@
+{
+ lib,
+ fleetLib,
+ config,
+ ...
+}: let
+ inherit (lib.options) mkOption;
+ inherit (lib.types) path;
+ inherit (lib.modules) mkRemovedOptionModule;
+ inherit (fleetLib.options) mkHostsOption;
+ inherit (fleetLib.types) listOfOverlay;
+
+ _file = ./nixpkgs.lib;
+in {
+ options = {
+ nixpkgs = {
+ buildUsing = mkOption {
+ description = ''
+ Default nixpkgs to use for building the systems.
+ '';
+ type = path;
+ };
+ overlays = mkOption {
+ description = ''
+ Package overlays to apply for all the hosts, gets propagated into
+ `hosts.*.nixosModules.nixpkgs.overlays`.
+ '';
+ type = listOfOverlay;
+ };
+ };
+ hosts = mkHostsOption {
+ inherit _file;
+ options.nixpkgs.buildUsing = mkOption {
+ description = ''
+ Nixpkgs to use for building the system.
+
+ Note that this option is defined at the host level, not the nixosModules level,
+ nixosModules will be evaluated using this flake input.
+ '';
+ type = path;
+ default = config.nixpkgs.buildUsing;
+ };
+ # imports = [
+ # (mkRemovedOptionModule ["nixpkgs" "overlays"] "this option needs to be specified at nixosModules level")
+ # ];
+ config.nixos = {
+ inherit _file;
+ nixpkgs.overlays = config.nixpkgs.overlays;
+ imports = [
+ (mkRemovedOptionModule ["nixpkgs" "buildUsing"] "this option should be specified at the host level, not the nixosModules level")
+ ];
+ };
+ };
+ };
+ config.nixpkgs.overlays = [
+ (final: prev: import ../../pkgs {inherit (final) callPackage craneLib;})
+ ];
+}
modules/fleet/secrets.nixdiffbeforeafterboth--- a/modules/fleet/secrets.nix
+++ b/modules/fleet/secrets.nix
@@ -4,9 +4,12 @@
config,
...
}: let
- inherit (fleetLib) hostsToAttrs;
- inherit (lib) mkOption mapAttrsToList mapAttrs filterAttrs concatStringsSep;
+ inherit (fleetLib.options) mkHostsOption;
+ inherit (lib.options) mkOption;
inherit (lib.types) lazyAttrsOf unspecified nullOr listOf str bool attrsOf submodule;
+ inherit (lib.lists) sort elem;
+ inherit (lib.attrsets) mapAttrsToList mapAttrs filterAttrs;
+ inherit (lib.strings) toJSON concatStringsSep;
sharedSecret = {config, ...}: {
freeformType = lazyAttrsOf unspecified;
@@ -82,11 +85,11 @@
};
};
};
+ inherit (config) hostSecrets sharedSecrets;
in {
options = {
version = mkOption {
type = str;
- default = "";
internal = true;
};
sharedSecrets = mkOption {
@@ -100,36 +103,34 @@
description = "Host secrets. Imported from fleet.nix";
internal = true;
};
+ hosts = mkHostsOption ({config, ...}: {
+ nixos = {
+ secrets = let
+ host = config._module.args.name;
+ processSecret = v:
+ (removeAttrs v ["createdAt" "expiresAt" "expectedOwners" "owners" "regenerateOnOwnerAdded" "regenerateOnOwnerRemoved"])
+ // {
+ shared = true;
+ };
+ in
+ (
+ mapAttrs (_: processSecret)
+ (filterAttrs (_: v: elem host v.owners) sharedSecrets)
+ )
+ // (mapAttrs (_: processSecret) (hostSecrets.${host} or {}));
+ _file = ./secrets.nix;
+ };
+ });
};
config = {
assertions =
mapAttrsToList
(name: secret: {
- assertion = secret.expectedOwners == null || builtins.sort (a: b: a < b) secret.owners == builtins.sort (a: b: a < b) secret.expectedOwners;
- message = "Shared secret ${name} is expected to be encrypted for ${builtins.toJSON secret.expectedOwners}, but it is encrypted for ${builtins.toJSON secret.owners}. Run fleet secrets regenerate to fix";
+ assertion = secret.expectedOwners == null || sort (a: b: a < b) secret.owners == sort (a: b: a < b) secret.expectedOwners;
+ message = "Shared secret ${name} is expected to be encrypted for ${toJSON secret.expectedOwners}, but it is encrypted for ${toJSON secret.owners}. Run fleet secrets regenerate to fix";
})
config.sharedSecrets;
- hosts = hostsToAttrs (host: {
- nixosModules = let
- # processPart
- processSecret = v:
- (removeAttrs v ["createdAt" "expiresAt" "expectedOwners" "owners" "regenerateOnOwnerAdded" "regenerateOnOwnerRemoved"])
- // {
- shared = true;
- };
- in [
- {
- secrets =
- (
- mapAttrs (_: processSecret)
- (filterAttrs (_: v: builtins.elem host v.owners) config.sharedSecrets)
- )
- // (mapAttrs (_: processSecret) (config.hostSecrets.${host} or {}));
- }
- ];
- });
- # TODO: Should this attribute be moved to `nixpkgs.overlays`?
- overlays = [
+ nixpkgs.overlays = [
(final: prev: {
mkSecretGenerators = {recipients}: rec {
# TODO: Merge both generators to one with consistent options syntax?
nixos/assertions.nixdiffbeforeafterboth--- /dev/null
+++ b/nixos/assertions.nix
@@ -0,0 +1,24 @@
+# Similar module exists for fleet, however it also defines assertions and warnings,
+# which are already defined for nixos.
+{
+ lib,
+ config,
+ ...
+}: let
+ inherit (lib.options) mkOption;
+ inherit (lib.lists) map filter;
+ inherit (lib.types) listOf str;
+in {
+ options = {
+ errors = mkOption {
+ type = listOf str;
+ internal = true;
+ description = ''
+ Similar to warnings, however build will fail if any error exists.
+ '';
+ };
+ };
+ config.errors =
+ map (v: v.message)
+ (filter (v: !v.assertion) config.assertions);
+}
nixos/meta.nixdiffbeforeafterboth--- a/nixos/meta.nix
+++ b/nixos/meta.nix
@@ -3,18 +3,16 @@
pkgs,
...
}: let
- inherit (lib) mkOption;
+ inherit (lib.options) mkOption;
inherit (lib.types) listOf str submodule;
+ inherit (lib.modules) mkRemovedOptionModule;
in {
options = {
- nixpkgs.resolvedPkgs = mkOption {
+ # TODO: Give a real name.
+ # Previously it was nixpkgs.resolvedPkgs, which was erroreously merged with nixpkgs override attribute.
+ _resolvedPkgs = mkOption {
type = lib.types.pkgs // {description = "nixpkgs.pkgs";};
description = "Value of pkgs";
- };
- tags = mkOption {
- type = listOf str;
- description = "Host tags";
- default = [];
};
network = mkOption {
type = submodule {
@@ -34,9 +32,11 @@
description = "Network definition of host";
};
};
+ imports = [
+ (mkRemovedOptionModule ["tags"] "tags are now defined at the host level, not the nixos system level for fast filtering without evaluating unnecessary hosts.")
+ ];
config = {
- tags = ["all"];
network = {};
- nixpkgs.resolvedPkgs = pkgs;
+ _resolvedPkgs = pkgs;
};
}
nixos/modules/module-list.nixdiffbeforeafterboth--- a/nixos/modules/module-list.nix
+++ b/nixos/modules/module-list.nix
@@ -1,4 +1,5 @@
[
+ ../assertions.nix
../meta.nix
../secrets.nix
../rollback.nix
nixos/nix-sign.nixdiffbeforeafterboth--- a/nixos/nix-sign.nix
+++ b/nixos/nix-sign.nix
@@ -1,7 +1,12 @@
# Required for nix copy in build_systems.rs
-{config, ...}: {
+{lib, config, ...}:
+let
+ inherit (lib.modules) mkIf;
+ hasPersistentHostname = config.networking.hostName != "";
+in
+{
# https://github.com/NixOS/nix/issues/3023
- systemd.services.generate-nix-cache-key = {
+ systemd.services.generate-nix-cache-key = mkIf hasPersistentHostname {
wantedBy = ["multi-user.target"];
serviceConfig.Type = "oneshot";
path = [config.nix.package];
@@ -10,5 +15,5 @@
nix-store --generate-binary-cache-key ${config.networking.hostName}-1 /etc/nix/private-key /etc/nix/public-key
'';
};
- nix.settings.secret-key-files = "/etc/nix/private-key";
+ nix.settings.secret-key-files = mkIf hasPersistentHostname "/etc/nix/private-key";
}
nixos/secrets.nixdiffbeforeafterboth--- a/nixos/secrets.nix
+++ b/nixos/secrets.nix
@@ -5,7 +5,11 @@
...
}: let
inherit (lib.strings) hasPrefix removePrefix;
- inherit (lib) mkOption mkOptionDefault mapAttrs stringAfter;
+ inherit (lib.stringsWithDeps) stringAfter;
+ inherit (lib.options) mkOption;
+ inherit (lib.lists) optional;
+ inherit (lib.attrsets) mapAttrs;
+ inherit (lib.modules) mkOptionDefault mkIf;
inherit (lib.types) submodule str attrsOf nullOr unspecified lazyAttrsOf;
plaintextPrefix = "<PLAINTEXT>";
plaintextNewlinePrefix = "<PLAINTEXT-NL>";
@@ -110,6 +114,7 @@
builtins.toJSON (mapAttrs (_: processSecret)
config.secrets);
};
+ useSysusers = (config.systemd ? sysusers && config.systemd.sysusers.enable) || (config ? userborn && config.userborn.enable);
in {
options = {
secrets = mkOption {
@@ -120,21 +125,44 @@
};
config = {
environment.systemPackages = [pkgs.fleet-install-secrets];
+
+ systemd.services.fleet-install-secrets = mkIf useSysusers {
+ wantedBy = ["sysinit.target"];
+ after = ["systemd-sysusers.service"];
+ restartTriggers = [
+ secretsFile
+ ];
+ aliases = [
+ "sops-install-secrets"
+ "agenix-install-secrets"
+ ];
+
+ unitConfig.DefaultDependencies = false;
+
+ serviceConfig = {
+ Type = "oneshot";
+ RemainAfterExit = true;
+ ExecStart = "${pkgs.fleet-install-secrets}/bin/fleet-install-secrets install ${secretsFile}";
+ };
+ };
system.activationScripts.decryptSecrets =
- stringAfter (
- [
- # secrets are owned by user/group, thus we need to refer to those
- "users"
- "groups"
- "specialfs"
- ]
- # nixos-impermanence compatibility: secrets are encrypted by host-key,
- # but with impermanence we expect that the host-key is installed by
- # persist-file activation script.
- ++ (lib.optional (config.system.activationScripts ? "persist-files") "persist-files")
- ) ''
- 1>&2 echo "setting up secrets"
- ${pkgs.fleet-install-secrets}/bin/fleet-install-secrets install ${secretsFile}
- '';
+ mkIf (!useSysusers)
+ (
+ stringAfter (
+ [
+ # secrets are owned by user/group, thus we need to refer to those
+ "users"
+ "groups"
+ "specialfs"
+ ]
+ # nixos-impermanence compatibility: secrets are encrypted by host-key,
+ # but with impermanence we expect that the host-key is installed by
+ # persist-file activation script.
+ ++ (optional (config.system.activationScripts ? "persist-files") "persist-files")
+ ) ''
+ 1>&2 echo "setting up secrets"
+ ${pkgs.fleet-install-secrets}/bin/fleet-install-secrets install ${secretsFile}
+ ''
+ );
};
}