Files
moon-nix/src/cli/config.mbt
T

73 lines
1.7 KiB
MoonBit

///|
/// Resolved global configuration for one invocation.
priv struct Config {
root : String // state dir: <root>/store, <root>/profile, <root>/cache
store_dir : String
profile : @profile.Profile
channel : @resolve.Channel
system : String
cache : @cache.Cache
}
///|
fn opt(m : @argparse.Matches, key : String) -> String? {
match m.values.get(key) {
Some([v, ..]) => Some(v)
_ => None
}
}
///|
async fn detect_system() -> String raise @store.NixError {
let (code, out, _) = @process.collect_output("uname", ["-m"]) catch {
e => raise Io("running uname: \{e}")
}
if code != 0 {
raise Io("uname -m exited with \{code}")
}
let arch = out.text().trim().to_owned() catch {
e => raise Io("decoding uname output: \{e}")
}
match arch {
"x86_64" => "x86_64-linux"
"aarch64" => "aarch64-linux"
a =>
raise Unsupported(
"unsupported machine architecture '\{a}' (use --system)",
)
}
}
///|
async fn load_config(m : @argparse.Matches) -> Config raise @store.NixError {
let root = match opt(m, "root") {
Some(r) => r
None =>
match @env.get_env_var("HOME") {
Some(h) => "\{h}/.mnix"
None => raise Io("HOME is not set; pass --root")
}
}
let store_dir = match opt(m, "store") {
Some(s) => s.trim_end(chars="/").to_owned()
None => "\{root}/store"
}
let channel = @resolve.Channel::parse(opt(m, "channel").unwrap())
let system = match opt(m, "system") {
Some(s) => s
None => detect_system()
}
let cache = match opt(m, "cache") {
Some(u) => @cache.Cache::new(url=u)
None => @cache.Cache::new()
}
{
root,
store_dir,
profile: { dir: "\{root}/profile", },
channel,
system,
cache,
}
}