@@ -18,6 +18,8 @@ let
1818
1919 prioOption = prio: optionalString (prio != null) " pri=${toString prio}";
2020
21+ specialFSTypes = [ "proc" "sysfs" "tmpfs" "devtmpfs" "devpts" ];
22+
2123 fileSystemOpts = { name, config, ... }: {
2224
2325 options = {
@@ -97,11 +99,22 @@ let
9799 description = "Disable running fsck on this filesystem.";
98100 };
99101
102+ early = mkOption {
103+ default = false;
104+ type = types.bool;
105+ internal = true;
106+ description = ''
107+ Mount this filesystem very early during boot. At the moment of
108+ mounting no disks are exposed, so this option is primarily for
109+ special file systems.
110+ '';
111+ };
112+
100113 };
101114
102115 config = {
103116 mountPoint = mkDefault name;
104− device = mkIf (config.fsType == "tmpfs") (mkDefault config.fsType);
117+ device = mkIf (elem config.fsType specialFSTypes) (mkDefault config.fsType);
105118 options = mkIf config.autoResize [ "x-nixos.autoresize" ];
106119
107120 # -F needed to allow bare block device without partitions
@@ -110,6 +123,13 @@ let
110123
111124 };
112125
126+ # Makes sequence of `specialMount device mountPoint options fsType` commands.
127+ # `systemMount` should be defined in the sourcing script.
128+ makeSpecialMounts = mounts:
129+ pkgs.writeText "mounts.sh" (concatMapStringsSep "\n" (mount: ''
130+ specialMount "${mount.device}" "${mount.mountPoint}" "${concatStringsSep "," mount.options}" "${mount.fsType}"
131+ '') mounts);
132+
113133in
114134
115135{
@@ -131,8 +151,7 @@ in
131151 "/bigdisk".label = "bigdisk";
132152 }
133153 '';
134− type = types.loaOf types.optionSet;
135− options = [ fileSystemOpts ];
154+ type = types.loaOf (types.submodule fileSystemOpts);
136155 description = ''
137156 The file systems to be mounted. It must include an entry for
138157 the root directory (<literal>mountPoint = "/"</literal>). Each
@@ -177,10 +196,14 @@ in
177196 { assertion = ! (fileSystems' ? "cycle");
178197 message = "The ‘fileSystems’ option can't be topologically sorted: mountpoint dependency path ${ls " -> " fileSystems'.cycle} loops to ${ls ", " fileSystems'.loops}";
179198 }
199+ { assertion = all (x: !x.early || (x.label == null && !x.autoFormat && !x.autoResize)) fileSystems;
200+ message = "Early filesystems don't support mounting by label, auto formatting and resizing";
201+ }
180202 ];
181203
182204 # Export for use in other modules
183205 system.build.fileSystems = fileSystems;
206+ system.build.earlyMountScript = makeSpecialMounts (filter (fs: fs.early) fileSystems);
184207
185208 boot.supportedFilesystems = map (fs: fs.fsType) fileSystems;
186209
@@ -211,7 +234,7 @@ in
211234 + " " + (if skipCheck fs then "0" else
212235 if fs.mountPoint == "/" then "1" else "2")
213236 + "\n"
214− ) fileSystems}
237+ ) (filter (fs: !fs.early) fileSystems)}
215238
216239 # Swap devices.
217240 ${flip concatMapStrings config.swapDevices (sw:
@@ -258,6 +281,16 @@ in
258281
259282 in listToAttrs (map formatDevice (filter (fs: fs.autoFormat) fileSystems));
260283
284+ # Sync mount options with systemd's src/core/mount-setup.c: mount_table.
285+ fileSystems = mapAttrs (n: fs: fs // { early = true; }) {
286+ "/proc" = { fsType = "proc"; options = [ "nosuid" "noexec" "nodev" ]; };
287+ "/sys" = { fsType = "sysfs"; options = [ "nosuid" "noexec" "nodev" ]; };
288+ "/run" = { fsType = "tmpfs"; options = [ "nosuid" "nodev" "strictatime" "mode=755" "size=${config.boot.runSize}" ]; };
289+ "/dev" = { fsType = "devtmpfs"; options = [ "nosuid" "strictatime" "mode=755" "size=${config.boot.devSize}" ]; };
290+ "/dev/shm" = { fsType = "tmpfs"; options = [ "nosuid" "nodev" "strictatime" "mode=1777" "size=${config.boot.devShmSize}" ]; };
291+ "/dev/pts" = { fsType = "devpts"; options = [ "nosuid" "noexec" "mode=620" "gid=${toString config.ids.gids.tty}" ]; };
292+ };
293+
261294 };
262295
263296}