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
use crate::{CONFIG, SYSCONFIG, CONFIGDIR};
use anyhow::{Result, anyhow};
use serde::{Deserialize, Serialize};
use std::{
fs::{self, File},
io::{Write, BufReader},
path::Path,
};
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
pub struct NixDataConfig {
pub systemconfig: Option<String>,
pub flake: Option<String>,
pub flakearg: Option<String>,
pub userpkgtype: Option<UserPkgType>
}
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
pub enum UserPkgType {
Profile,
Env,
}
pub fn getconfig() -> Result<NixDataConfig> {
if Path::new(&*CONFIG).exists() {
let config: NixDataConfig = serde_json::from_reader(BufReader::new(File::open(&*CONFIG)?))?;
Ok(config)
} else if Path::new(SYSCONFIG).exists() {
let config: NixDataConfig = serde_json::from_reader(BufReader::new(File::open(SYSCONFIG)?))?;
Ok(config)
} else {
Err(anyhow!("No config file found"))
}
}
pub fn setuserconfig(config: NixDataConfig) -> Result<()> {
if !Path::new(&*CONFIGDIR).exists() {
fs::create_dir_all(&*CONFIGDIR)?;
}
let mut file = File::create(&*CONFIG)?;
file.write_all(serde_json::to_string_pretty(&config)?.as_bytes())?;
Ok(())
}