-
Notifications
You must be signed in to change notification settings - Fork 58
/
place_nix_configuration.rs
139 lines (122 loc) · 4.73 KB
/
place_nix_configuration.rs
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use tracing::{span, Span};
use crate::action::base::create_or_merge_nix_config::CreateOrMergeNixConfigError;
use crate::action::base::{CreateDirectory, CreateOrMergeNixConfig};
use crate::action::{Action, ActionDescription, ActionError, ActionTag, StatefulAction};
const NIX_CONF_FOLDER: &str = "/etc/nix";
const NIX_CONF: &str = "/etc/nix/nix.conf";
/**
Place the `/etc/nix.conf` file
*/
#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]
pub struct PlaceNixConfiguration {
create_directory: StatefulAction<CreateDirectory>,
create_or_merge_nix_config: StatefulAction<CreateOrMergeNixConfig>,
}
impl PlaceNixConfiguration {
#[tracing::instrument(level = "debug", skip_all)]
pub async fn plan(
nix_build_group_name: String,
extra_conf: Vec<String>,
force: bool,
) -> Result<StatefulAction<Self>, ActionError> {
let extra_conf = extra_conf.join("\n");
let mut nix_config = nix_config_parser::NixConfig::parse_string(extra_conf, None)
.map_err(CreateOrMergeNixConfigError::ParseNixConfig)
.map_err(|e| ActionError::Custom(Box::new(e)))?;
let settings = nix_config.settings_mut();
settings.insert("build-users-group".to_string(), nix_build_group_name);
settings.insert(
"experimental-features".to_string(),
"nix-command flakes".to_string(),
);
settings.insert("auto-optimise-store".to_string(), "true".to_string());
settings.insert(
"bash-prompt-prefix".to_string(),
"(nix:$name)\\040".to_string(),
);
settings.insert(
"extra-nix-path".to_string(),
"nixpkgs=flake:nixpkgs".to_string(),
);
let create_directory = CreateDirectory::plan(NIX_CONF_FOLDER, None, None, 0o0755, force)
.await
.map_err(|e| ActionError::Child(CreateDirectory::action_tag(), Box::new(e)))?;
let create_or_merge_nix_config = CreateOrMergeNixConfig::plan(NIX_CONF, nix_config)
.await
.map_err(|e| {
ActionError::Child(CreateOrMergeNixConfig::action_tag(), Box::new(e))
})?;
Ok(Self {
create_directory,
create_or_merge_nix_config,
}
.into())
}
}
#[async_trait::async_trait]
#[typetag::serde(name = "place_nix_configuration")]
impl Action for PlaceNixConfiguration {
fn action_tag() -> ActionTag {
ActionTag("place_nix_configuration")
}
fn tracing_synopsis(&self) -> String {
format!("Place the Nix configuration in `{NIX_CONF}`")
}
fn tracing_span(&self) -> Span {
span!(tracing::Level::DEBUG, "place_nix_configuration",)
}
fn execute_description(&self) -> Vec<ActionDescription> {
let Self {
create_or_merge_nix_config,
create_directory,
} = self;
let mut explanation = vec![
"This file is read by the Nix daemon to set its configuration options at runtime."
.to_string(),
];
if let Some(val) = create_directory.describe_execute().iter().next() {
explanation.push(val.description.clone())
}
for val in create_or_merge_nix_config.describe_execute().iter() {
explanation.push(val.description.clone())
}
vec![ActionDescription::new(self.tracing_synopsis(), explanation)]
}
#[tracing::instrument(level = "debug", skip_all)]
async fn execute(&mut self) -> Result<(), ActionError> {
self.create_directory
.try_execute()
.await
.map_err(|e| ActionError::Child(self.create_directory.action_tag(), Box::new(e)))?;
self.create_or_merge_nix_config
.try_execute()
.await
.map_err(|e| {
ActionError::Child(self.create_or_merge_nix_config.action_tag(), Box::new(e))
})?;
Ok(())
}
fn revert_description(&self) -> Vec<ActionDescription> {
vec![ActionDescription::new(
format!("Remove the Nix configuration in `{NIX_CONF}`"),
vec![
"This file is read by the Nix daemon to set its configuration options at runtime."
.to_string(),
],
)]
}
#[tracing::instrument(level = "debug", skip_all)]
async fn revert(&mut self) -> Result<(), ActionError> {
self.create_or_merge_nix_config
.try_revert()
.await
.map_err(|e| {
ActionError::Child(self.create_or_merge_nix_config.action_tag(), Box::new(e))
})?;
self.create_directory
.try_revert()
.await
.map_err(|e| ActionError::Child(self.create_directory.action_tag(), Box::new(e)))?;
Ok(())
}
}