Move the profile to the system level.

This commit is contained in:
R-VdP 2023-02-08 14:31:09 +00:00
parent 1620a81d15
commit 8f31818a27
No known key found for this signature in database
2 changed files with 20 additions and 19 deletions

View file

@ -2,7 +2,7 @@ use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;
use std::{env, fs, process, str};
use std::{fs, process, str};
use super::{create_store_link, StorePath, FLAKE_ATTR, PROFILE_NAME};
@ -14,13 +14,8 @@ struct NixBuildOutput {
}
pub fn generate(flake_uri: &str) -> Result<()> {
let user = env::var("USER")?;
// TODO: we temporarily put this under per-user to avoid needing root access
// we will move this to /nix/var/nix/profiles/ later on.
let profiles_dir = format!("profiles/per-user/{}", user);
let gcroots_dir = format!("gcroots/per-user/{}", user);
let profile_path = format!("/nix/var/nix/{}/{}", profiles_dir, PROFILE_NAME);
let gcroot_path = format!("/nix/var/nix/{}/{}-current", gcroots_dir, PROFILE_NAME);
let profile_path = format!("/nix/var/nix/profiles/{}", PROFILE_NAME);
let gcroot_path = format!("/nix/var/nix/gcroots/{}-current", PROFILE_NAME);
// FIXME: we should not hard-code the system here
let flake_attr = format!("{}.x86_64-linux", FLAKE_ATTR);

View file

@ -1,3 +1,5 @@
use std::process::ExitCode;
use anyhow::{anyhow, Result};
use clap::Parser;
@ -22,29 +24,33 @@ enum Action {
},
}
fn main() {
fn main() -> ExitCode {
let args = Args::parse();
// FIXME: set default level to info
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
handle_toplevel_error(go(args.action))
}
match args.action {
Action::Activate { store_path } => handle_toplevel_error(activate(store_path)),
Action::Generate { flake_uri } => {
handle_toplevel_error(service_manager::generate::generate(&flake_uri))
}
fn go(action: Action) -> Result<()> {
check_root()?;
match action {
Action::Activate { store_path } => service_manager::activate::activate(store_path),
Action::Generate { flake_uri } => service_manager::generate::generate(&flake_uri),
}
}
fn activate(store_path: StorePath) -> Result<()> {
fn check_root() -> Result<()> {
if !nix::unistd::Uid::is_root(nix::unistd::getuid()) {
return Err(anyhow!("We need root permissions."));
}
service_manager::activate::activate(store_path)
Ok(())
}
fn handle_toplevel_error<T>(r: Result<T>) {
if let Err(e) = r {
log::error!("{}", e)
fn handle_toplevel_error<T>(r: Result<T>) -> ExitCode {
if let Err(e) = &r {
log::error!("{}", e);
return ExitCode::FAILURE;
}
ExitCode::SUCCESS
}