Skip to content

Commit e1a9892

Browse files
committed
Add a NixOS target
1 parent 208d5bc commit e1a9892

File tree

9 files changed

+373
-75
lines changed

9 files changed

+373
-75
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ zdaemon = "0.0.2"
3232
zfilexfer = "0.0.2"
3333
hostname = "0.1"
3434
czmq = { version = "0.1", optional = true }
35+
interfaces = "0.0.1"
36+
ipnetwork = "0.12"
3537

3638
[lib]
3739
name = "inapi"

src/error.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use std::ffi::CString;
1919
use zdaemon;
2020
#[cfg(feature = "remote-run")]
2121
use zfilexfer;
22+
use interfaces;
2223

2324
pub type Result<T> = result::Result<T, Error>;
2425

@@ -96,6 +97,8 @@ pub enum Error {
9697
#[cfg(feature = "remote-run")]
9798
/// ZFileXfer error
9899
ZFileXfer(zfilexfer::Error),
100+
/// Network interface error
101+
Interfaces(interfaces::InterfacesError),
99102
}
100103

101104
unsafe impl Send for Error {}
@@ -135,6 +138,7 @@ impl fmt::Display for Error {
135138
Error::ZDaemon(ref e) => write!(f, "ZDaemon error: {}", e),
136139
#[cfg(feature = "remote-run")]
137140
Error::ZFileXfer(ref e) => write!(f, "ZFileXfer error: {}", e),
141+
Error::Interfaces(ref e) => write!(f, "Interfaces error: {}", e),
138142
}
139143
}
140144
}
@@ -174,6 +178,7 @@ impl error::Error for Error {
174178
Error::ZDaemon(ref e) => e.description(),
175179
#[cfg(feature = "remote-run")]
176180
Error::ZFileXfer(ref e) => e.description(),
181+
Error::Interfaces(ref e) => e.description(),
177182
}
178183
}
179184
}
@@ -278,6 +283,12 @@ impl convert::From<zfilexfer::Error> for Error {
278283
}
279284
}
280285

286+
impl From<interfaces::InterfacesError> for Error {
287+
fn from(err: interfaces::InterfacesError) -> Error {
288+
Error::Interfaces(err)
289+
}
290+
}
291+
281292
#[cfg(feature = "remote-run")]
282293
#[derive(Debug)]
283294
pub struct MissingFrame {

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ extern crate tempfile;
4242
extern crate zdaemon;
4343
extern crate zfilexfer;
4444
extern crate hostname;
45+
extern crate interfaces;
46+
extern crate ipnetwork;
4547

4648
#[macro_use]
4749
mod ffi_helpers;

src/package/providers/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub mod macports;
1313
pub mod pkg;
1414
pub mod ports;
1515
pub mod yum;
16+
pub mod nix;
1617

1718
use command::CommandResult;
1819
use error::{Error, Result};
@@ -32,6 +33,7 @@ pub enum Providers {
3233
Pkg,
3334
Ports,
3435
Yum,
36+
Nix,
3537
}
3638

3739
impl ToString for Providers {
@@ -44,6 +46,7 @@ impl ToString for Providers {
4446
&Providers::Pkg => "Pkg".to_string(),
4547
&Providers::Ports => "Ports".to_string(),
4648
&Providers::Yum => "Yum".to_string(),
49+
&Providers::Nix => "Nix".to_string(),
4750
}
4851
}
4952
}
@@ -58,6 +61,7 @@ impl convert::From<String> for Providers {
5861
"Pkg" => Providers::Pkg,
5962
"Ports" => Providers::Ports,
6063
"Yum" => Providers::Yum,
64+
"Nix" => Providers::Nix,
6165
_ => panic!("Invalid provider"),
6266
}
6367
}
@@ -91,6 +95,7 @@ impl ProviderFactory {
9195
Providers::Pkg => Box::new(pkg::Pkg),
9296
Providers::Ports => Box::new(ports::Ports),
9397
Providers::Yum => Box::new(yum::Yum),
98+
Providers::Nix => Box::new(nix::Nix),
9499
}
95100
}
96101
}

src/package/providers/nix.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2015-2017 Intecture Developers. See the COPYRIGHT file at the
2+
// top-level directory of this distribution and at
3+
// https://intecture.io/COPYRIGHT.
4+
//
5+
// Licensed under the Mozilla Public License 2.0 <LICENSE or
6+
// https://www.tldrlegal.com/l/mpl-2.0>. This file may not be copied,
7+
// modified, or distributed except according to those terms.
8+
9+
//! Nix package provider
10+
11+
use command::{Command, CommandResult};
12+
use error::Result;
13+
use host::Host;
14+
use super::*;
15+
16+
pub struct Nix;
17+
18+
impl Provider for Nix {
19+
fn get_providers(&self) -> Providers {
20+
Providers::Nix
21+
}
22+
23+
fn is_active(&self, host: &mut Host) -> Result<bool> {
24+
let cmd = Command::new("which nix-env");
25+
let result = try!(cmd.exec(host));
26+
27+
Ok(result.exit_code == 0)
28+
}
29+
30+
fn is_installed(&self, host: &mut Host, name: &str) -> Result<bool> {
31+
let cmd = Command::new(&format!("nix-env --install --dry-run {}", name));
32+
let result = try!(cmd.exec(host));
33+
34+
if result.exit_code != 0 {
35+
return Err(Error::Agent(result.stderr));
36+
}
37+
38+
Ok(!result.stderr.contains("these paths will be fetched"))
39+
}
40+
41+
fn install(&self, host: &mut Host, name: &str) -> Result<CommandResult> {
42+
let cmd = Command::new(&format!("nix-env --install {}", name));
43+
cmd.exec(host)
44+
}
45+
46+
fn uninstall(&self, host: &mut Host, name: &str) -> Result<CommandResult> {
47+
let cmd = Command::new(&format!("nix-env --uninstall {}", name));
48+
cmd.exec(host)
49+
}
50+
}

src/target/linux.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use super::debian::DebianTarget;
2424
use super::fedora::FedoraTarget;
2525
use super::redhat::RedhatTarget;
2626
use super::ubuntu::UbuntuTarget;
27+
use super::nixos::NixOsTarget;
2728
use host::telemetry::TelemetryTarget;
2829

2930
static mut LINUX_PLATFORM: LinuxPlatform = LinuxPlatform::Centos;
@@ -35,6 +36,7 @@ enum LinuxPlatform {
3536
Fedora,
3637
Redhat,
3738
Ubuntu,
39+
NixOs,
3840
}
3941

4042
//
@@ -49,6 +51,7 @@ impl CommandTarget for Target {
4951
&LinuxPlatform::Fedora => FedoraTarget::exec(host, cmd),
5052
&LinuxPlatform::Redhat => RedhatTarget::exec(host, cmd),
5153
&LinuxPlatform::Ubuntu => UbuntuTarget::exec(host, cmd),
54+
&LinuxPlatform::NixOs => NixOsTarget::exec(host, cmd),
5255
}
5356
}
5457
}
@@ -65,6 +68,7 @@ impl<P: AsRef<Path>> DirectoryTarget<P> for Target {
6568
&LinuxPlatform::Fedora => FedoraTarget::directory_is_directory(host, path),
6669
&LinuxPlatform::Redhat => RedhatTarget::directory_is_directory(host, path),
6770
&LinuxPlatform::Ubuntu => UbuntuTarget::directory_is_directory(host, path),
71+
&LinuxPlatform::NixOs => NixOsTarget::directory_is_directory(host, path),
6872
}
6973
}
7074

@@ -75,6 +79,7 @@ impl<P: AsRef<Path>> DirectoryTarget<P> for Target {
7579
&LinuxPlatform::Fedora => FedoraTarget::directory_exists(host, path),
7680
&LinuxPlatform::Redhat => RedhatTarget::directory_exists(host, path),
7781
&LinuxPlatform::Ubuntu => UbuntuTarget::directory_exists(host, path),
82+
&LinuxPlatform::NixOs => NixOsTarget::directory_exists(host, path),
7883
}
7984
}
8085

@@ -85,6 +90,7 @@ impl<P: AsRef<Path>> DirectoryTarget<P> for Target {
8590
&LinuxPlatform::Fedora => FedoraTarget::directory_create(host, path, recursive),
8691
&LinuxPlatform::Redhat => RedhatTarget::directory_create(host, path, recursive),
8792
&LinuxPlatform::Ubuntu => UbuntuTarget::directory_create(host, path, recursive),
93+
&LinuxPlatform::NixOs => NixOsTarget::directory_create(host, path, recursive),
8894
}
8995
}
9096

@@ -95,6 +101,7 @@ impl<P: AsRef<Path>> DirectoryTarget<P> for Target {
95101
&LinuxPlatform::Fedora => FedoraTarget::directory_delete(host, path, recursive),
96102
&LinuxPlatform::Redhat => RedhatTarget::directory_delete(host, path, recursive),
97103
&LinuxPlatform::Ubuntu => UbuntuTarget::directory_delete(host, path, recursive),
104+
&LinuxPlatform::NixOs => NixOsTarget::directory_delete(host, path, recursive),
98105
}
99106
}
100107

@@ -105,6 +112,7 @@ impl<P: AsRef<Path>> DirectoryTarget<P> for Target {
105112
&LinuxPlatform::Fedora => FedoraTarget::directory_mv(host, path, new_path),
106113
&LinuxPlatform::Redhat => RedhatTarget::directory_mv(host, path, new_path),
107114
&LinuxPlatform::Ubuntu => UbuntuTarget::directory_mv(host, path, new_path),
115+
&LinuxPlatform::NixOs => NixOsTarget::directory_mv(host, path, new_path),
108116
}
109117
}
110118

@@ -115,6 +123,7 @@ impl<P: AsRef<Path>> DirectoryTarget<P> for Target {
115123
&LinuxPlatform::Fedora => FedoraTarget::directory_get_owner(host, path),
116124
&LinuxPlatform::Redhat => RedhatTarget::directory_get_owner(host, path),
117125
&LinuxPlatform::Ubuntu => UbuntuTarget::directory_get_owner(host, path),
126+
&LinuxPlatform::NixOs => NixOsTarget::directory_get_owner(host, path),
118127
}
119128
}
120129

@@ -125,6 +134,7 @@ impl<P: AsRef<Path>> DirectoryTarget<P> for Target {
125134
&LinuxPlatform::Fedora => FedoraTarget::directory_set_owner(host, path, user, group),
126135
&LinuxPlatform::Redhat => RedhatTarget::directory_set_owner(host, path, user, group),
127136
&LinuxPlatform::Ubuntu => UbuntuTarget::directory_set_owner(host, path, user, group),
137+
&LinuxPlatform::NixOs => NixOsTarget::directory_set_owner(host, path, user, group),
128138
}
129139
}
130140

@@ -135,6 +145,7 @@ impl<P: AsRef<Path>> DirectoryTarget<P> for Target {
135145
&LinuxPlatform::Fedora => FedoraTarget::directory_get_mode(host, path),
136146
&LinuxPlatform::Redhat => RedhatTarget::directory_get_mode(host, path),
137147
&LinuxPlatform::Ubuntu => UbuntuTarget::directory_get_mode(host, path),
148+
&LinuxPlatform::NixOs => NixOsTarget::directory_get_mode(host, path),
138149
}
139150
}
140151

@@ -145,6 +156,7 @@ impl<P: AsRef<Path>> DirectoryTarget<P> for Target {
145156
&LinuxPlatform::Fedora => FedoraTarget::directory_set_mode(host, path, mode),
146157
&LinuxPlatform::Redhat => RedhatTarget::directory_set_mode(host, path, mode),
147158
&LinuxPlatform::Ubuntu => UbuntuTarget::directory_set_mode(host, path, mode),
159+
&LinuxPlatform::NixOs => NixOsTarget::directory_set_mode(host, path, mode),
148160
}
149161
}
150162
}
@@ -161,6 +173,7 @@ impl<P: AsRef<Path>> FileTarget<P> for Target {
161173
&LinuxPlatform::Fedora => FedoraTarget::file_is_file(host, path),
162174
&LinuxPlatform::Redhat => RedhatTarget::file_is_file(host, path),
163175
&LinuxPlatform::Ubuntu => UbuntuTarget::file_is_file(host, path),
176+
&LinuxPlatform::NixOs => NixOsTarget::file_is_file(host, path),
164177
}
165178
}
166179

@@ -171,6 +184,7 @@ impl<P: AsRef<Path>> FileTarget<P> for Target {
171184
&LinuxPlatform::Fedora => FedoraTarget::file_exists(host, path),
172185
&LinuxPlatform::Redhat => RedhatTarget::file_exists(host, path),
173186
&LinuxPlatform::Ubuntu => UbuntuTarget::file_exists(host, path),
187+
&LinuxPlatform::NixOs => NixOsTarget::file_exists(host, path),
174188
}
175189
}
176190

@@ -181,6 +195,7 @@ impl<P: AsRef<Path>> FileTarget<P> for Target {
181195
&LinuxPlatform::Fedora => FedoraTarget::file_delete(host, path),
182196
&LinuxPlatform::Redhat => RedhatTarget::file_delete(host, path),
183197
&LinuxPlatform::Ubuntu => UbuntuTarget::file_delete(host, path),
198+
&LinuxPlatform::NixOs => NixOsTarget::file_delete(host, path),
184199
}
185200
}
186201

@@ -191,6 +206,7 @@ impl<P: AsRef<Path>> FileTarget<P> for Target {
191206
&LinuxPlatform::Fedora => FedoraTarget::file_mv(host, path, new_path),
192207
&LinuxPlatform::Redhat => RedhatTarget::file_mv(host, path, new_path),
193208
&LinuxPlatform::Ubuntu => UbuntuTarget::file_mv(host, path, new_path),
209+
&LinuxPlatform::NixOs => NixOsTarget::file_mv(host, path, new_path),
194210
}
195211
}
196212

@@ -201,6 +217,7 @@ impl<P: AsRef<Path>> FileTarget<P> for Target {
201217
&LinuxPlatform::Fedora => FedoraTarget::file_copy(host, path, new_path),
202218
&LinuxPlatform::Redhat => RedhatTarget::file_copy(host, path, new_path),
203219
&LinuxPlatform::Ubuntu => UbuntuTarget::file_copy(host, path, new_path),
220+
&LinuxPlatform::NixOs => NixOsTarget::file_copy(host, path, new_path),
204221
}
205222
}
206223

@@ -211,6 +228,7 @@ impl<P: AsRef<Path>> FileTarget<P> for Target {
211228
&LinuxPlatform::Fedora => FedoraTarget::file_get_owner(host, path),
212229
&LinuxPlatform::Redhat => RedhatTarget::file_get_owner(host, path),
213230
&LinuxPlatform::Ubuntu => UbuntuTarget::file_get_owner(host, path),
231+
&LinuxPlatform::NixOs => NixOsTarget::file_get_owner(host, path),
214232
}
215233
}
216234

@@ -221,6 +239,7 @@ impl<P: AsRef<Path>> FileTarget<P> for Target {
221239
&LinuxPlatform::Fedora => FedoraTarget::file_set_owner(host, path, user, group),
222240
&LinuxPlatform::Redhat => RedhatTarget::file_set_owner(host, path, user, group),
223241
&LinuxPlatform::Ubuntu => UbuntuTarget::file_set_owner(host, path, user, group),
242+
&LinuxPlatform::NixOs => NixOsTarget::file_set_owner(host, path, user, group),
224243
}
225244
}
226245

@@ -231,6 +250,7 @@ impl<P: AsRef<Path>> FileTarget<P> for Target {
231250
&LinuxPlatform::Fedora => FedoraTarget::file_get_mode(host, path),
232251
&LinuxPlatform::Redhat => RedhatTarget::file_get_mode(host, path),
233252
&LinuxPlatform::Ubuntu => UbuntuTarget::file_get_mode(host, path),
253+
&LinuxPlatform::NixOs => NixOsTarget::file_get_mode(host, path),
234254
}
235255
}
236256

@@ -241,6 +261,7 @@ impl<P: AsRef<Path>> FileTarget<P> for Target {
241261
&LinuxPlatform::Fedora => FedoraTarget::file_set_mode(host, path, mode),
242262
&LinuxPlatform::Redhat => RedhatTarget::file_set_mode(host, path, mode),
243263
&LinuxPlatform::Ubuntu => UbuntuTarget::file_set_mode(host, path, mode),
264+
&LinuxPlatform::NixOs => NixOsTarget::file_set_mode(host, path, mode),
244265
}
245266
}
246267
}
@@ -257,6 +278,7 @@ impl PackageTarget for Target {
257278
&LinuxPlatform::Fedora => FedoraTarget::default_provider(host),
258279
&LinuxPlatform::Redhat => RedhatTarget::default_provider(host),
259280
&LinuxPlatform::Ubuntu => UbuntuTarget::default_provider(host),
281+
&LinuxPlatform::NixOs => NixOsTarget::default_provider(host),
260282
}
261283
}
262284
}
@@ -273,6 +295,7 @@ impl ServiceTarget for Target {
273295
&LinuxPlatform::Fedora => FedoraTarget::service_action(host, name, action),
274296
&LinuxPlatform::Redhat => RedhatTarget::service_action(host, name, action),
275297
&LinuxPlatform::Ubuntu => UbuntuTarget::service_action(host, name, action),
298+
&LinuxPlatform::NixOs => NixOsTarget::service_action(host, name, action),
276299
}
277300
}
278301
}
@@ -289,6 +312,7 @@ impl TelemetryTarget for Target {
289312
&LinuxPlatform::Fedora => FedoraTarget::telemetry_init(host),
290313
&LinuxPlatform::Redhat => RedhatTarget::telemetry_init(host),
291314
&LinuxPlatform::Ubuntu => UbuntuTarget::telemetry_init(host),
315+
&LinuxPlatform::NixOs => NixOsTarget::telemetry_init(host),
292316
}
293317
}
294318
}
@@ -314,6 +338,10 @@ fn fingerprint_os() -> &'static LinuxPlatform {
314338
// RedHat
315339
else if let Ok(_) = fs::metadata("/etc/redhat-release") {
316340
unsafe { LINUX_PLATFORM = LinuxPlatform::Redhat; }
341+
}
342+
// NixOS
343+
else if let Ok(_) = fs::metadata("/etc/nixos/configuration.nix") {
344+
unsafe { LINUX_PLATFORM = LinuxPlatform::NixOs; }
317345
} else {
318346
panic!("Unknown Linux distro");
319347
}

0 commit comments

Comments
 (0)