I'm running a VM on a virtual cluster. The (experimental) cluster provisioning system puts an unformatted block device in /dev/sdb. Upon cluster initialization, I want to format this device and mount.

In the initialization workflow, my VM contacts the cluster manager to learn how configure the VM--network interfaces, and devices. I'm feeding this info to a nix expression which my /etc/nixos/configuration.nix inherits. I can make the filesystem mount a partitioned disk:

fileSystems."/log" = { device = "${logDevice}1";
                       fsType = "bind"; };

However, I don't know how to format the device as a part of configuration. If this was a nix derivation, I would put make some sort of buildHook, e.g.

formatLogDisk = ''
   parted -s ${logDevice} mklabel gpt
   parted -s ${logDevice} unit % mkpart extended ext4 0 100
   mkfs.ext4 -q ${logDevice}1

So I'm wondering if there is some sort of preMount module hook that might run this. Otherwise, I can run the utilities directly during the phase that collects the system information, but I was hoping thered be a nixOs based answer.

  • It sounds a bit dangerous to always format a drive before mounting it. I mean what should happen when the system gets rebooted and it tries to mount a second time? Aug 24, 2019 at 9:18
  • Its fine in this case. The drive is explicitly used for temporary data—experiment logs. Aug 24, 2019 at 13:04
  • Have you tried using an activation script? These scripts run early in the boot process. Sep 1, 2019 at 1:35
  • Emmanuel thanks. I hadn’t heard of that. Turns out the fileSystems option has an autoformat option which looks like it should work—but i need to test. No partitioning, but that doesn’t look to be necessary. Sep 1, 2019 at 2:09

1 Answer 1


I have not used it myself, but there is an autoFormat option.

       If the device does not currently contain a filesystem (as determined
       by blkid, then automatically format it with the filesystem type specified
       in fsType. Use with caution.

       Type: boolean

       Default: false

       Declared by:

Something along these lines should work. Note that it's would be formatting the whole disk (which is presumably virtual anyway) without creating partitions on it first like your script does. I'm not sure if there are settings to auto-partition a disk.

fileSystems."/log" = { device = "${logDevice}";
                       fsType = "ext4";
                       autoFormat = true; };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.