envhub_pkgs/
nix.rs

1use crate::PackageManager;
2use anyhow::Error;
3use std::{env, process::Command};
4use users::get_current_username;
5
6pub struct Nix {}
7
8impl Nix {
9    pub fn new() -> Self {
10        Self {}
11    }
12}
13
14impl PackageManager for Nix {
15    fn install(&self, _name: &str) -> Result<(), Error> {
16        self.setup()?;
17        todo!();
18    }
19
20    fn uninstall(&self, _name: &str) -> Result<(), Error> {
21        todo!()
22    }
23
24    fn setup(&self) -> Result<(), Error> {
25        let user = match get_current_username() {
26            Some(user) => user.to_string_lossy().to_string(),
27            None => "root".to_string(),
28        };
29
30        env::set_var("USER", user);
31        env::set_var(
32            "PATH",
33            format!(
34                "{}:{}",
35                env::var("PATH")?,
36                "/nix/var/nix/profiles/default/bin"
37            ),
38        );
39        let mut child = Command::new("sh")
40            .arg("-c")
41            .arg("type systemctl > /dev/null 2> /dev/null")
42            .spawn()?;
43        let status = child.wait()?;
44        let init = match status.code() {
45            Some(0) => "",
46            _ => "--init none",
47        };
48
49        let linux = match std::env::consts::OS {
50            "linux" => format!("linux --extra-conf 'sandbox = false' {}", init),
51            _ => "".to_string(),
52        };
53        let mut child = Command::new("sh")
54            .arg("-c")
55            .arg(format!("type nix > /dev/null || curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install {}", linux))
56            .spawn()?;
57        child.wait()?;
58
59        let mut child = Command::new("sh")
60            .arg("-c")
61            .arg(format!("type nix > /dev/null || curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install {} --no-confirm", linux))
62            .spawn()?;
63        child.wait()?;
64        Ok(())
65    }
66}