Replacing the bootStage2

I’m trying to create a Linux Container (LXC) that starts up, performs a task, and then immediately shuts itself down.

This is already working fairly nicely with nixos-generators and the configuration at:

However, systemd seems rather overkill here. It seems neater if I could overwrite myself, do just the minimal initialization needed for the script (enabling networking), performing the task and shutting down again.

Naively setting in configuration.nix doesn’t seem to work, though. Is this possible? How would I do it?

This sounds like a case for not using NixOS: NixOS is quite tightly coupled with systemd, but you can make your container’s store contain just your init script and replace nixos with it, something like

{ nixpkgs ? <nixpkgs> }:
  pkgs = import nixpkgs {}; 
  initScript = pkgs.writeScript "init" ''
    ${pkgs.dhcp}/bin/dhclient -v eth0
    exec ${mastodon-bot-hack42}/bin/mastodon-bot # or some lightweight service manager
in import (nixpkgs + "/nixos/lib/make-system-tarball.nix") {
  contents = [
    { source = initScript; target = "/init"; }
  extraArgs = "--owner=0";
  extraCommands = "mkdir -p proc sys dev";

That will get you a tarball containing a symlink at /init to the init script defined there (the make-system-tarball part of this is mostly copied from the lxc generator itself at

1 Like

Thanks for that, I’ll be sure to look into that further!

Do you mean “using Nix but not NixOS”? That indeed perhaps could be interesting. I wouldn’t want to give up Nix - right now even if it means pulling in systemd it’s worth it IMHO :smiley: .

1 Like

Yes, exactly — Nix without NixOS :slight_smile: systemd will probably be pulled in as a dependency because the dhcp client will (maybe transitively) depend on udev or whatever, but you can avoid running it as PID1 in this way.

1 Like

Thanks again, based on this I cut the image down to a pretty nice 42MB (compressed),

(most of the remaining noise there is going into compiling down the clojurescript to javascript so we don’t need to ship the lumo interpreter in the image)