Infinite recursion when creating bind mounts for certain systemd services

Hello, I’m trying to make an impermanence setup easier to maintain but keep getting infinite recursion errors. Here’s a brief sketch of what I’m trying to do:

For systemd services, I’m adding a boolean option of whether state should be persisted,

options.systemd.services = lib.mkOption {
    type = lib.types.attrsOf (lib.types.submodule {
      options.persist = {
        enable = lib.mkEnableOption "persist service state";
      };
    });
  };

config.tailscaled.persist.enable = true;

and then for each service I want to persist, I’m bind mounting the systemd StateDirectory to somewhere persistent.

config.fileSystems =
  let
    persistentServices = lib.filterAttrs (name: service: service.persist.enable) config.systemd.services;

    mkBindMountNameValuePair = stateDir: {
      name = "/var/lib/${stateDir}";
      value = {
        device = "/persist/var/lib/${stateDir}";
        options = [ "bind" "x-gvfs-hide" ];
      };
    };
  in
    lib.listToAttrs (lib.mapAttrsToList (name: service: mkBindMountNameValuePair service.serviceConfig.StateDirectory) persistentServices);

The trouble is this doesn’t work and I get an infinite recursion encounted error. This seems unsuprising because I am reading from config.systemd.services to create the bind mounts, and then presumably NixOS is creating some systemd services/units to actually mount the bind mounts. But I would think that because I am filtering services using my persist.enable option which defaults to false, the configuration should converge to a fixed point and build okay. But this doesn’t happen. Any hints of what I can do to fix this?

1 Like

No, NixOS does not create a service to mount bind mounts. It’s just a regular ole entry in /etc/fstab. I am genuinely bewildered that this leads to infinite recursion.

If systemd.services in any way depends on fileSystems, then you’ll get an infinite recursion error. And it does. A simple grep -r fileSystems nixos/modules shows the culprit: nixos/modules/services/network-filesystems/orangefs/{client,server}.nix