Explaining: Block device file-system auto-creation on NixOS

I wrote an article “Block device file-system auto-creation on NixOS”.

It explains in some detail and with links to the code how declarative block-device initialisation in NixOS / NixOps works.

Quoting the summary here:

NixOS already contains all the functionality needed to auto-format and auto-mount block devices upon attachment.

For example, to do it with an AWS EBS volume, you only need:

  fileSystems."/data" = {
    autoFormat = true;
    fsType = "ext4";
    device = "/dev/xvdb";

  services.udev.extraRules = ''
    ACTION=="add", SUBSYSTEM=="block", KERNEL=="xvdb", ENV{SYSTEMD_WANTS}+="${utils.escapeSystemdPath "/data"}.mount"

Read the full article, or suggest improvements, here:

1 Like

I wonder why you needed the udev rule.

Systemd creates .device units using udev and .mount's have implicit dependencies on them. You should not have to add a udev rule for the .mount to show up :open_mouth: