1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use std::{
    env::consts::OS,
    process::{Command, ExitStatus, Stdio},
    sync::mpsc::Sender,
};

use crate::{exec, nix::Nix, Extension};
use anyhow::Error;
use fluentci_types::Output;

#[derive(Default)]
pub struct Flox {}

impl Extension for Flox {
    fn exec(
        &mut self,
        cmd: &str,
        tx: Sender<String>,
        out: Output,
        last_cmd: bool,
        work_dir: &str,
    ) -> Result<ExitStatus, Error> {
        self.setup()?;

        if cmd.is_empty() {
            return Ok(ExitStatus::default());
        }

        Command::new("bash")
            .arg("-c")
            .arg("[ -d .flox ] || flox init")
            .current_dir(work_dir)
            .stdout(Stdio::inherit())
            .stderr(Stdio::inherit())
            .spawn()?
            .wait()?;

        let cmd = format!("flox activate -- {}", cmd);
        exec(&cmd, tx, out, last_cmd, work_dir)
    }

    fn setup(&self) -> Result<(), Error> {
        Nix::default().setup()?;

        let status = Command::new("sh")
            .arg("-c")
            .arg("type flox > /dev/null")
            .spawn()?
            .wait()?;

        if status.success() {
            return Ok(());
        }

        let sudo = Command::new("sh")
            .arg("-c")
            .arg("type sudo > /dev/null")
            .spawn()?
            .wait()?;

        let sudo = if sudo.success() { "sudo" } else { "" };

        Command::new("sh")
            .arg("-c")
            .arg(&format!(
                "echo \"trusted-users = root $USER\" | {} tee -a /etc/nix/nix.conf",
                sudo
            ))
            .stdout(Stdio::inherit())
            .stderr(Stdio::inherit())
            .spawn()?
            .wait()?;

        let sudo = if OS == "macos" { "sudo" } else { "" };

        Command::new("sh")
            .arg("-c")
            .arg(&format!("echo 'extra-trusted-substituters = https://cache.floxdev.com' | {} tee -a /etc/nix/nix.conf && echo 'extra-trusted-public-keys = flox-cache-public-1:7F4OyH7ZCnFhcze3fJdfyXYLQw/aV7GEed86nQ7IsOs=' | {} tee -a /etc/nix/nix.conf", sudo, sudo))
            .stdout(Stdio::inherit())
            .stderr(Stdio::inherit())
            .spawn()?
            .wait()?;

        Command::new("sh")
            .arg("-c")
            .arg(&format!(
                "{} nix profile install --impure \
                --experimental-features 'nix-command flakes' \
                --accept-flake-config \
                github:flox/flox",
                sudo
            ))
            .stdout(Stdio::inherit())
            .stderr(Stdio::inherit())
            .spawn()?
            .wait()?;

        Ok(())
    }

    fn format_command(&self, cmd: &str) -> String {
        format!("[ -d .flox ] || flox init ; flox activate -- {}", cmd)
    }
}