Nix flake configuration manager
at main 63 lines 1.8 kB view raw
1use serde::{Deserialize, Serialize}; 2use std::fs; 3use std::path::PathBuf; 4 5#[derive(Debug, Clone, Deserialize, Serialize)] 6pub struct Config { 7 pub commands: Commands, 8 pub flake: FlakeConfig, 9} 10 11#[derive(Debug, Clone, Deserialize, Serialize)] 12pub struct Commands { 13 pub nixos: String, 14 pub home_manager: String, 15 pub colmena: String, 16} 17 18#[derive(Debug, Clone, Deserialize, Serialize)] 19pub struct FlakeConfig { 20 /// Path to the flake (default: ".") 21 pub path: String, 22 /// Default execution mode when multiple configs are selected 23 pub parallel: bool, 24} 25 26impl Default for Config { 27 fn default() -> Self { 28 Config { 29 commands: Commands { 30 nixos: "nixos-rebuild switch --flake .#{name}".into(), 31 home_manager: "home-manager switch --flake .#{name}".into(), 32 colmena: "cd {path} && colmena apply --on {name}".into(), 33 }, 34 flake: FlakeConfig { 35 path: ".".into(), 36 parallel: false, 37 }, 38 } 39 } 40} 41 42pub fn config_path() -> PathBuf { 43 let config_dir = std::env::var("XDG_CONFIG_HOME") 44 .map(PathBuf::from) 45 .unwrap_or_else(|_| { 46 PathBuf::from(std::env::var("HOME").unwrap_or_else(|_| ".".into())).join(".config") 47 }); 48 config_dir.join("lumar").join("config.toml") 49} 50 51pub fn load_or_default() -> Config { 52 let path = config_path(); 53 if path.exists() { 54 match fs::read_to_string(&path) { 55 Ok(content) => match toml::from_str(&content) { 56 Ok(cfg) => return cfg, 57 Err(e) => eprintln!("Warning: failed to parse config at {}: {e}", path.display()), 58 }, 59 Err(e) => eprintln!("Warning: failed to read config at {}: {e}", path.display()), 60 } 61 } 62 Config::default() 63}