~ruther/nix-tmpactivator

58dd1f391141cb0b92f9582d803e9510a13e1bbe — Rutherther 11 months ago ceb0d40
feat: add self management (auto activation, deactivation)
2 files changed, 84 insertions(+), 37 deletions(-)

M modules/home.nix
M modules/tmpfiles.nix
M modules/home.nix => modules/home.nix +1 -0
@@ 46,6 46,7 @@ in {
  };

  config = lib.mkIf (config.home.user != null) {
    tmpfiles.selfManagement.targetDir = "${config.home.homeDirectory}/.local/share/user-tmpfiles.d";
    tmpfiles.defaultUser = config.home.user;
    tmpfiles.defaultGroup = config.home.group;


M modules/tmpfiles.nix => modules/tmpfiles.nix +83 -37
@@ 7,6 7,20 @@ let
in {
  options = {
    tmpfiles = {
      selfManagement = {
        enable = lib.mkEnableOption "tmpfiles self management";
        targetDir = lib.mkOption {
          type = lib.types.str;
        };

        configurationPackage = lib.mkOption {
          type = lib.types.package;
          description = ''
            This package contains the tmpfiles configuration package
          '';
        };
      };

      defaultUser = lib.mkOption {
        type = lib.types.nullOr lib.types.str;
      };


@@ 42,42 56,74 @@ in {
    };
  };

  config = {
    # assertions = builtins.map
    #   (file: {
    #     assertion = file.source == null || file.text == null;
    #     message = "Either text or source can be set, not both.";
    #   })
    # tmpFiles;
    #

    tmpfiles.configurationFileLines = builtins.map (file: (tmpLib.mkTmpFile ({
      type = file.type;
      target = file.target;
      mode = file.mode;
      user = if file.user == null then config.tmpfiles.defaultUser else file.user;
      group = if file.group == null then config.tmpfiles.defaultGroup else file.group;
      contents = if file.source != null then file.source else file.text;
    }))) tmpFiles;

    tmpfiles.configurationPackage = pkgs.writeTextFile {
      name = "100-tmpactivator.conf";
      destination = "/lib/tmpfiles.d/100-tmpactivator.conf";
      text = lib.concatStringsSep "\n" config.tmpfiles.configurationFileLines;
    };
  config = lib.mkMerge [
    (lib.mkIf config.tmpfiles.selfManagement.enable {
      tmpfiles.selfManagement.configurationPackage = pkgs.writeTextFile {
        name = "99-tmpactivator.conf";
        destination = "/share/user-tmpfiles.d/99-tmpactivator.conf";
        text = ''
          L+ ${config.tmpfiles.selfManagement.targetDir}/100-tmpactivator-files.conf - - - - ${config.tmpfiles.configurationPackage}/share/user-tmpfiles.d/100-tmpactivator-files.conf
          r ${config.tmpfiles.selfManagement.targetDir}/100-tmpactivator-files.conf - - - - -
        '';
      };

    tmpfiles.activationPackage = pkgs.symlinkJoin {
      name = "tmpfiles-activation";

      paths = [
        (pkgs.writeShellScriptBin "activate" ''
          systemd-tmpfiles --create "${config.tmpfiles.configurationPackage}/lib/tmpfiles.d/100-tmpactivator.conf"
        '')
        (pkgs.writeShellScriptBin "deactivate" ''
          systemd-tmpfiles --remove "${config.tmpfiles.configurationPackage}/lib/tmpfiles.d/100-tmpactivator.conf"
        '')
        config.tmpfiles.configurationPackage
      ];
    };
  };
      tmpfiles.activationPackage = pkgs.symlinkJoin {
        name = "tmpfiles-activation";

        paths = [
          (pkgs.writeShellScriptBin "activate" ''
            systemd-tmpfiles --create "${config.tmpfiles.selfManagement.configurationPackage}/share/user-tmpfiles.d/99-tmpactivator.conf"
            systemd-tmpfiles --create "${config.tmpfiles.configurationPackage}/share/user-tmpfiles.d/100-tmpactivator-files.conf"
          '')
          (pkgs.writeShellScriptBin "deactivate" ''
            systemd-tmpfiles --remove "${config.tmpfiles.configurationPackage}/share/user-tmpfiles.d/100-tmpactivator-files.conf"
            systemd-tmpfiles --remove "${config.tmpfiles.selfManagement.configurationPackage}/share/user-tmpfiles.d/99-tmpactivator.conf"
          '')
          config.tmpfiles.configurationPackage
          config.tmpfiles.selfManagement.configurationPackage
        ];
      };
    })

    (lib.mkIf (!config.tmpfiles.selfManagement.enable) {
      tmpfiles.activationPackage = pkgs.symlinkJoin {
        name = "tmpfiles-activation";

        paths = [
          (pkgs.writeShellScriptBin "activate" ''
            systemd-tmpfiles --create "${config.tmpfiles.configurationPackage}/share/user-tmpfiles.d/100-tmpactivator-files.conf"
          '')
          (pkgs.writeShellScriptBin "deactivate" ''
            systemd-tmpfiles --remove "${config.tmpfiles.configurationPackage}/share/user-tmpfiles.d/100-tmpactivator-files.conf"
          '')
          config.tmpfiles.configurationPackage
        ];
      };
    })

    {
      # assertions = builtins.map
      #   (file: {
      #     assertion = file.source == null || file.text == null;
      #     message = "Either text or source can be set, not both.";
      #   })
      # tmpFiles;
      #

      tmpfiles.configurationFileLines = builtins.map (file: (tmpLib.mkTmpFile ({
        type = file.type;
        target = file.target;
        mode = file.mode;
        user = if file.user == null then config.tmpfiles.defaultUser else file.user;
        group = if file.group == null then config.tmpfiles.defaultGroup else file.group;
        contents = if file.source != null then file.source else file.text;
      }))) tmpFiles;

      tmpfiles.configurationPackage = pkgs.writeTextFile {
        name = "100-tmpactivator-files.conf";
        destination = "/share/user-tmpfiles.d/100-tmpactivator-files.conf";
        text = lib.concatStringsSep "\n" config.tmpfiles.configurationFileLines;
      };
    }
  ];
}

Do not follow this link