Skip to content

Commit

Permalink
Merge branch 'main' into auto_update_deps
Browse files Browse the repository at this point in the history
  • Loading branch information
r-vdp authored Oct 12, 2023
2 parents 4324005 + 447eeb3 commit 903fe97
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 22 deletions.
1 change: 1 addition & 0 deletions examples/example.nix
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,6 @@
'';
})
);
systemd.tmpfiles.rules = [ "D /var/tmp/system-manager 0755 root root -" ];
};
}
2 changes: 1 addition & 1 deletion nix/lib.nix
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ in
'';

passthru = {
runVM = hostPkgs.writeShellScriptBin "run-vm"
driverInteractive = hostPkgs.writeShellScriptBin "run-vm"
(defaultTest {
extraDriverArgs = "--interactive";
});
Expand Down
1 change: 1 addition & 0 deletions nix/modules/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
./environment.nix
./etc.nix
./systemd.nix
./tmpfiles.nix
./upstream/nixpkgs
];

Expand Down
27 changes: 27 additions & 0 deletions nix/modules/tmpfiles.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{ config, lib, ... }:
let
inherit (lib) types;
in
{
options = {
systemd.tmpfiles.rules = lib.mkOption {
type = types.listOf types.str;
default = [ ];
example = [ "d /tmp 1777 root root 10d" ];
description = lib.mdDoc ''
Rules for creation, deletion and cleaning of volatile and temporary files
automatically. See
{manpage}`tmpfiles.d(5)`
for the exact format.
'';
};
};

config = {
environment.etc."tmpfiles.d/00-system-manager.conf".text = ''
# This file is created automatically and should not be modified.
# Please change the option ‘systemd.tmpfiles.rules’ instead.
${lib.concatStringsSep "\n" config.systemd.tmpfiles.rules}
'';
};
}
14 changes: 14 additions & 0 deletions src/activate.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod etc_files;
mod services;
mod tmp_files;

use anyhow::Result;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -80,6 +81,17 @@ pub fn activate(store_path: &StorePath, ephemeral: bool) -> Result<()> {

match etc_files::activate(store_path, old_state.file_tree, ephemeral) {
Ok(etc_tree) => {
log::info!("Activating tmp files...");
match tmp_files::activate() {
Ok(_) => {
log::debug!("Successfully created tmp files");
}
Err(e) => {
log::error!("Error during activation of tmp files");
log::error!("{e}");
}
};

log::info!("Activating systemd services...");
match services::activate(store_path, old_state.services, ephemeral) {
Ok(services) => State {
Expand All @@ -104,6 +116,7 @@ pub fn activate(store_path: &StorePath, ephemeral: bool) -> Result<()> {
}
}
.write_to_file(state_file)?;

Ok(())
}

Expand Down Expand Up @@ -184,6 +197,7 @@ pub fn deactivate() -> Result<()> {
}
}
.write_to_file(state_file)?;

Ok(())
}

Expand Down
29 changes: 29 additions & 0 deletions src/activate/tmp_files.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::activate;

use super::ActivationResult;
use std::process;

type TmpFilesActivationResult = ActivationResult<()>;

pub fn activate() -> TmpFilesActivationResult {
let mut cmd = process::Command::new("systemd-tmpfiles");
cmd.arg("--create")
.arg("--remove")
.arg("/etc/tmpfiles.d/00-system-manager.conf");
let output = cmd
.stdout(process::Stdio::inherit())
.stderr(process::Stdio::inherit())
.output()
.expect("Error forking process");

output.status.success().then_some(()).ok_or_else(|| {
activate::ActivationError::WithPartialResult {
result: (),
source: anyhow::anyhow!(
"Error while creating tmpfiles\nstdout: {}\nstderr: {}",
String::from_utf8_lossy(output.stdout.as_ref()),
String::from_utf8_lossy(output.stderr.as_ref())
),
}
})
}
55 changes: 34 additions & 21 deletions test/nix/modules/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,24 @@ forEachUbuntuImage
node1.wait_for_unit("system-manager.target")
node1.succeed("systemctl status service-9.service")
node1.succeed("cat /etc/baz/bar/foo2")
node1.succeed("cat /etc/a/nested/example/foo3")
node1.succeed("cat /etc/foo.conf")
node1.succeed("test -f /etc/baz/bar/foo2")
node1.succeed("test -f /etc/a/nested/example/foo3")
node1.succeed("test -f /etc/foo.conf")
node1.succeed("grep -F 'launch_the_rockets = true' /etc/foo.conf")
node1.fail("grep -F 'launch_the_rockets = false' /etc/foo.conf")
node1.succeed("test -d /var/tmp/system-manager")
${system-manager.lib.activateProfileSnippet { node = "node1"; profile = newConfig; }}
node1.succeed("systemctl status new-service.service")
node1.fail("systemctl status service-9.service")
node1.fail("cat /etc/a/nested/example/foo3")
node1.fail("cat /etc/baz/bar/foo2")
node1.fail("cat /etc/systemd/system/nginx.service")
node1.succeed("cat /etc/foo_new")
node1.fail("test -f /etc/a/nested/example/foo3")
node1.fail("test -f /etc/baz/bar/foo2")
node1.fail("test -f /etc/systemd/system/nginx.service")
node1.succeed("test -f /etc/foo_new")
node1.succeed("test -d /var/tmp/system-manager")
node1.succeed("touch /var/tmp/system-manager/foo1")
# Simulate a reboot, to check that the services defined with
# system-manager start correctly after a reboot.
Expand All @@ -152,13 +157,14 @@ forEachUbuntuImage
node1.succeed("systemctl status new-service.service")
node1.fail("systemctl status service-9.service")
node1.fail("cat /etc/a/nested/example/foo3")
node1.fail("cat /etc/baz/bar/foo2")
node1.succeed("cat /etc/foo_new")
node1.fail("test -f /etc/a/nested/example/foo3")
node1.fail("test -f /etc/baz/bar/foo2")
node1.succeed("test -f /etc/foo_new")
${system-manager.lib.deactivateProfileSnippet { node = "node1"; profile = newConfig; }}
node1.fail("systemctl status new-service.service")
node1.fail("cat /etc/foo_new")
node1.fail("test -f /etc/foo_new")
#node1.fail("test -f /var/tmp/system-manager/foo1")
'';
})
];
Expand Down Expand Up @@ -197,29 +203,36 @@ forEachUbuntuImage
node1.wait_for_unit("default.target")
${system-manager.lib.activateProfileSnippet { node = "node1"; }}
${system-manager.lib.prepopulateProfileSnippet { node = "node1"; }}
node1.systemctl("daemon-reload")
node1.systemctl("start default.target")
# Simulate a reboot, to check that the services defined with
# system-manager start correctly after a reboot.
# TODO: can we find an easy way to really reboot the VM and not
# loose the root FS state?
node1.systemctl("isolate rescue.target")
# We need to send a return character to dismiss the rescue-mode prompt
node1.send_key("ret")
node1.systemctl("isolate default.target")
node1.wait_for_unit("system-manager.target")
node1.succeed("systemctl status service-9.service")
node1.succeed("cat /etc/baz/bar/foo2")
node1.succeed("cat /etc/a/nested/example/foo3")
node1.succeed("cat /etc/foo.conf")
node1.succeed("test -f /etc/baz/bar/foo2")
node1.succeed("test -f /etc/a/nested/example/foo3")
node1.succeed("test -f /etc/foo.conf")
node1.succeed("grep -F 'launch_the_rockets = true' /etc/foo.conf")
node1.fail("grep -F 'launch_the_rockets = false' /etc/foo.conf")
${system-manager.lib.activateProfileSnippet { node = "node1"; profile = newConfig; }}
node1.succeed("systemctl status new-service.service")
node1.fail("systemctl status service-9.service")
node1.fail("cat /etc/a/nested/example/foo3")
node1.fail("cat /etc/baz/bar/foo2")
node1.succeed("cat /etc/foo_new")
node1.fail("test -f /etc/a/nested/example/foo3")
node1.fail("test -f /etc/baz/bar/foo2")
node1.succeed("test -f /etc/foo_new")
${system-manager.lib.deactivateProfileSnippet { node = "node1"; profile = newConfig; }}
node1.fail("systemctl status new-service.service")
node1.fail("cat /etc/foo_new")
node1.fail("test -f /etc/foo_new")
'';
}
)
Expand Down

0 comments on commit 903fe97

Please sign in to comment.