Skip to content

Instantly share code, notes, and snippets.

@erikarvstedt
Last active July 19, 2018 12:27
Show Gist options
  • Save erikarvstedt/3017979f8eba758967fe7d57a996b4be to your computer and use it in GitHub Desktop.
Save erikarvstedt/3017979f8eba758967fe7d57a996b4be to your computer and use it in GitHub Desktop.
containerName=dev
dir=/tmp/$containerName
if [[ $EUID != 0 ]]; then
echo "This script must be run as root"
exit 1
fi
nixos-container create $containerName
mkdir -p $dir
service=$dir/container@$containerName.service
cp /etc/systemd/system/container@.service $service
chmod a+w $service
mkdir -p /etc/systemd-mutable/system
ln -s $service /etc/systemd-mutable/system
# Copy the service scripts to $dir and replace the paths in $service with the copies
$(nix-build --no-out-link '<nixpkgs>' -A ruby)/bin/ruby - $service $dir <<'EOF'
require 'fileutils'
service, dir = ARGV
src = File.read(service)
['ExecStart', 'ExecStartPre', 'ExecStartPost', 'ExecStop'].each do |script|
src = src.gsub(/^(#{script}=)(\S+)/) do |_|
prefix = $1
script_source = $2
mutable_source = File.join(dir, script)
FileUtils.cp(script_source, mutable_source)
FileUtils.chmod('+w', mutable_source)
"#{prefix}#{mutable_source}"
end
end
File.write(service, src)
EOF
systemctl daemon-reload
echo "You can now edit these files to change the service"
printf '%s\n' $dir/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment