Skip to content

Instantly share code, notes, and snippets.

@yorickvP
Last active October 21, 2020 09:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yorickvP/6feabed071268b3030c4c2507244d392 to your computer and use it in GitHub Desktop.
Save yorickvP/6feabed071268b3030c4c2507244d392 to your computer and use it in GitHub Desktop.
build-portabled-image
{ pkgs ? import <nixpkgs> {}, runCommandNoCC ? pkgs.runCommandNoCC }:
{
name,
config,
extraMountDirs ? [],
extraMountFiles ? []
}:
let
eval-config = import (pkgs.path + "/nixos/lib/eval-config.nix");
etc =
(eval-config { modules = [
(pkgs.path + "/nixos/modules/profiles/minimal.nix")
{
boot.isContainer = true;
environment.etc."os-release".text = ''PORTABLE_PRETTY_NAME="${name} Portable Service"'';
}
config
];
}).config.system.build.etc;
in
# todo: mount paths
runCommandNoCC "portable-image-${name}" {} ''
mkdir -p $out/etc/systemd
cp -L ${etc}/etc/os-release $out/etc
${pkgs.rsync}/bin/rsync -a --copy-unsafe-links ${etc}/etc/systemd/system $out/etc/systemd
mkdir -p $out/{bin,dev,etc,nix/store,proc,root,run,sys,tmp,usr,var/tmp,var/lib/walkthroughd}
touch $out/etc/{machine-id,resolv.conf}
''
nix-build walkthroughd-portable.nix -o walkthroughd_nix
PORTABLECTL=$(nix-build systemd)/lib/systemd/portablectl
sudo $PORTABLECTL attach ./walkthroughd_nix --runtime
sudo systemctl start walkthroughd
systemctl status walkthroughd
sudo systemctl stop walkthroughd
sudo $PORTABLECTL detach ./walkthroughd_nix
{ config, ...}: {
# systemd-portabled
systemd.additionalUpstreamSystemUnits = [
"dbus-org.freedesktop.portable1.service"
"systemd-portabled.service"
];
environment.etc."systemd/portable/profile" = {
source = "${config.systemd.package}/lib/systemd/portable/profile";
};
}
{ pkgs ? import <nixpkgs> {} }:
let buildPortableImage = pkgs.callPackage ./build-portable-image.nix {}; in
buildPortableImage {
name = "walkthroughd";
config = {
systemd.services.walkthroughd = {
description = "A simple example service";
serviceConfig.BindReadOnlyPaths = "/nix/store"; # todo: fix somewhere else
wantedBy = [ "multi-user.target" ]; # todo: make working
serviceConfig.ExecStart = "${(pkgs.callPackage ./walkthroughd.nix {})}/bin/walkthroughd";
};
};
}
{ stdenv, fetchFromGitHub }:
stdenv.mkDerivation {
name = "walkthroughd";
src = fetchFromGitHub {
owner = "systemd";
"repo"= "portable-walkthrough";
"rev"= "ac70be525d5d9645111c6c4bf28882bb76be08ea";
"sha256"= "115c92s7bslpajjf6vlysq894xizf72g6kpyx3wpr141maci8z2y";
};
buildCommand = ''
mkdir -p $out/bin/
gcc -o $out/bin/walkthroughd $src/walkthroughd.c
'';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment