Skip to content

NixOS

mold provides a NixOS module for declarative server and Discord bot deployment.

Flake Setup

Add mold to your flake inputs and import the module:

nix
{
  inputs.mold.url = "github:utensils/mold";

  outputs = { self, nixpkgs, mold, ... }: {
    nixosConfigurations.myhost = nixpkgs.lib.nixosSystem {
      modules = [
        mold.nixosModules.default
        ./mold.nix  # your mold config (see below)
      ];
    };
  };
}

Minimal Configuration

nix
{ inputs, system, ... }:
{
  services.mold = {
    enable = true;
    package = inputs.mold.packages.${system}.default;  # Ada / RTX 40-series
  };
}

This starts mold serve on port 7680 with sensible defaults, creates a mold system user, and manages the data directory at /var/lib/mold.

Full Configuration Example

nix
{ inputs, system, config, ... }:
{
  services.mold = {
    enable = true;

    # Package — must match your GPU architecture
    package = inputs.mold.packages.${system}.default;     # Ada (RTX 4090, sm_89)
    # package = inputs.mold.packages.${system}.mold-sm120; # Blackwell (RTX 5090, sm_120)

    # Advisory hint — emits a build warning if package doesn't match
    # cudaArch = "blackwell";

    # Server
    port = 7680;
    bindAddress = "0.0.0.0";
    logLevel = "info";         # trace, debug, info, warn, error
    openFirewall = false;      # set true to allow LAN access

    # Directories
    homeDir = "/var/lib/mold";           # MOLD_HOME
    # modelsDir = "/var/lib/mold/models"; # defaults to homeDir/models

    # Models
    defaultModel = "flux2-klein:q8";

    # Image persistence — save copies of all server-generated images
    # outputDir = "/srv/mold/gallery";

    # CORS — restrict to specific origin (null = permissive)
    # corsOrigin = "https://mysite.example.com";

    # HuggingFace auth — for gated model repos
    # Points to a file containing the token (e.g. agenix secret)
    hfTokenFile = config.age.secrets.hf-token.path;

    # Extra environment variables
    environment = {
      MOLD_EAGER = "1";        # keep all components loaded
      MOLD_T5_VARIANT = "q4";  # use Q4 T5 encoder
    };

    # Discord bot
    discord = {
      enable = true;
      # Must be an EnvironmentFile: MOLD_DISCORD_TOKEN=your-token-here
      tokenFile = config.age.secrets.discord-token.path;
      # moldHost = "http://localhost:7680";  # defaults to main server
      cooldownSeconds = 10;
      logLevel = "info";
    };
  };
}

Module Options Reference

Server Options

OptionTypeDefaultDescription
enableboolfalseEnable the mold server
packagepackageThe mold package (must set explicitly)
cudaArchnull/enumnull"ada" or "blackwell" — advisory warning only
portport7680HTTP server port
bindAddressstring"0.0.0.0"Address to bind
homeDirstring"/var/lib/mold"Base directory (MOLD_HOME)
modelsDirstringhomeDir + /modelsModel storage directory
logLevelenum"info"Log level (trace/debug/info/warn/error)
corsOriginnull/stringnullCORS origin restriction (null = permissive)
openFirewallboolfalseOpen firewall port
defaultModelnull/stringnullDefault model name
outputDirnull/stringnullImage output directory (default: homeDir/output)
hfTokenFilenull/pathnullPath to file with HuggingFace token
environmentattrs{}Extra environment variables

Discord Bot Options

OptionTypeDefaultDescription
discord.enableboolfalseEnable Discord bot service
discord.packagepackageconfig.services.mold.packagePackage for the bot
discord.tokenFilepathFile containing bot token
discord.moldHoststring"http://localhost:{port}"mold server URL
discord.cooldownSecondsint10Per-user generation cooldown
discord.logLevelenum"info"Bot log level

What the Module Creates

  • System user mold:mold with home at homeDir
  • Directories via tmpfiles: homeDir, modelsDir, and outputDir (if set)
  • Systemd service mold.service — runs mold serve with:
    • LD_LIBRARY_PATH=/run/opengl-driver/lib for NixOS CUDA driver access
    • video and render supplementary groups for GPU access
    • Hardened: NoNewPrivileges, ProtectSystem=full, ProtectHome, PrivateTmp
    • HuggingFace token loaded via EnvironmentFile (never in process env)
  • Systemd service mold-discord.service (if discord.enable) — runs mold discord, depends on mold.service, further hardened with ProtectSystem=strict and PrivateDevices (no GPU needed)
  • Firewall rule if openFirewall = true

GPU Architecture

The module cannot auto-select the flake package — you must set package to match your GPU:

GPUPackage
RTX 40-series (Ada)inputs.mold.packages.${system}.default
RTX 50-series (Blackwell)inputs.mold.packages.${system}.mold-sm120

Set cudaArch = "blackwell" as a reminder — it emits a build warning if you forget to switch the package.

Build Variants

bash
nix build github:utensils/mold
bash
nix build github:utensils/mold#mold-sm120

Development Shell

bash
nix develop github:utensils/mold

The devshell includes Rust toolchain, CUDA toolkit, and convenience commands:

CommandDescription
buildcargo build (debug)
build-releasecargo build --release
build-serverBuild with GPU + preview + discord
serveStart the mold server
generateGenerate an image
moldRun any mold CLI command
checkcargo check
clippycargo clippy
fmtcargo fmt
run-testscargo test
coverageTest coverage report
docs-devStart VitePress docs dev server
docs-buildBuild the documentation site
docs-fmtFormat docs with prettier