<commit_before>{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.railcar;
  generateUnit = name: containerConfig:
    let
      container = pkgs.ociTools.buildContainer {
        args = [
          (pkgs.writeShellScript "run.sh" containerConfig.cmd).outPath
        ];
      };
    in
      nameValuePair "railcar-${name}" {
        enable = true;
        wantedBy = [ "multi-user.target" ];
        serviceConfig = {
            ExecStart = ''
              ${cfg.package}/bin/railcar -r ${cfg.stateDir} run ${name} -b ${container}
            '';
            Type = containerConfig.runType;
          };
      };
  mount = with types; (submodule {
    options = {
      type = mkOption {
        type = string;
        default = "none";
        description = ''
          The type of the filesystem to be mounted.
          Linux: filesystem types supported by the kernel as listed in 
          `/proc/filesystems` (e.g., "minix", "ext2", "ext3", "jfs", "xfs", 
          "reiserfs", "msdos", "proc", "nfs", "iso9660"). For bind mounts 
          (when options include either bind or rbind), the type is a dummy,
          often "none" (not listed in /proc/filesystems).
        '';
      };
      source = mkOption {
        type = string;
        description = "Source for the in-container mount";
      };
      options = mkOption {
        type = loaOf (string);
        default = [ "bind" ];
        description = ''
          Mount options of the filesystem to be used.
        
          Support optoions are listed in the mount(8) man page. Note that 
          both filesystem-independent and filesystem-specific options 
          are listed.
        '';
      };
    };
  });
in
{
  options.services.railcar = {
    enable = mkEnableOption "railcar";

    containers = mkOption {
      default = {};
      description = "Declarative container configuration";
      type = with types; loaOf (submodule ({ name, config, ... }: {
        options = {
          cmd = mkOption {
            type = types.string;
            description = "Command or script to run inside the container";
          };

          mounts = mkOption {
            type = with types; attrsOf mount;
            default = {};
            description = ''
              A set of mounts inside the container.

              The defaults have been chosen for simple bindmounts, meaning
              that you only need to provide the "source" parameter.
            '';
            example = ''
              { "/data" = { source = "/var/lib/data"; }; }
            '';
          };

          runType = mkOption {
            type = types.string;
            default = "oneshot";
            description = "The systemd service run type";
          };

          os = mkOption {
            type = types.string;
            default = "linux";
            description = "OS type of the container";
          };

          arch = mkOption {
            type = types.string;
            default = "x86_64";
            description = "Computer architecture type of the container";
          };
        };
      }));
    };

    stateDir = mkOption {
      type = types.path;<commit_msg>nixos/railcar: remove use of the deprecated string type<commit_after>{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.railcar;
  generateUnit = name: containerConfig:
    let
      container = pkgs.ociTools.buildContainer {
        args = [
          (pkgs.writeShellScript "run.sh" containerConfig.cmd).outPath
        ];
      };
    in
      nameValuePair "railcar-${name}" {
        enable = true;
        wantedBy = [ "multi-user.target" ];
        serviceConfig = {
            ExecStart = ''
              ${cfg.package}/bin/railcar -r ${cfg.stateDir} run ${name} -b ${container}
            '';
            Type = containerConfig.runType;
          };
      };
  mount = with types; (submodule {
    options = {
      type = mkOption {
        type = str;
        default = "none";
        description = ''
          The type of the filesystem to be mounted.
          Linux: filesystem types supported by the kernel as listed in 
          `/proc/filesystems` (e.g., "minix", "ext2", "ext3", "jfs", "xfs", 
          "reiserfs", "msdos", "proc", "nfs", "iso9660"). For bind mounts 
          (when options include either bind or rbind), the type is a dummy,
          often "none" (not listed in /proc/filesystems).
        '';
      };
      source = mkOption {
        type = str;
        description = "Source for the in-container mount";
      };
      options = mkOption {
        type = loaOf (str);
        default = [ "bind" ];
        description = ''
          Mount options of the filesystem to be used.
        
          Support optoions are listed in the mount(8) man page. Note that 
          both filesystem-independent and filesystem-specific options 
          are listed.
        '';
      };
    };
  });
in
{
  options.services.railcar = {
    enable = mkEnableOption "railcar";

    containers = mkOption {
      default = {};
      description = "Declarative container configuration";
      type = with types; loaOf (submodule ({ name, config, ... }: {
        options = {
          cmd = mkOption {
            type = types.lines;
            description = "Command or script to run inside the container";
          };

          mounts = mkOption {
            type = with types; attrsOf mount;
            default = {};
            description = ''
              A set of mounts inside the container.

              The defaults have been chosen for simple bindmounts, meaning
              that you only need to provide the "source" parameter.
            '';
            example = ''
              { "/data" = { source = "/var/lib/data"; }; }
            '';
          };

          runType = mkOption {
            type = types.str;
            default = "oneshot";
            description = "The systemd service run type";
          };

          os = mkOption {
            type = types.str;
            default = "linux";
            description = "OS type of the container";
          };

          arch = mkOption {
            type = types.str;
            default = "x86_64";
            description = "Computer architecture type of the container";
          };
        };
      }));
    };

    stateDir = mkOption {
      type = types.path;
{'fraction_non_alphanumeric': 0.11046784540760445, 'fraction_numerical': 0.0034423407917383822, 'mean_word_length': 1.5619238476953907, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5887301', 'n_tokens_mistral': 1732, 'n_tokens_neox': 1671, 'n_words': 518}