Files
nixconf/seaweedfs-mount.nix
2025-10-12 15:53:51 +02:00

116 lines
3.7 KiB
Nix

# /etc/nixos/modules/seaweedfs-mount.nix
#
# This module sets up a systemd service to mount a SeaweedFS filer
# via FUSE directly on the NixOS host.
{ config, pkgs, lib, ... }:
let
# Use lib.mkOption to create configurable settings for our module.
# This makes it reusable and easy to adjust from configuration.nix.
cfg = config.services.seaweedfs-mount;
in
{
# Define the options that can be set in configuration.nix
options.services.seaweedfs-mount = {
enable = lib.mkEnableOption "SeaweedFS FUSE mount service";
filerAddresses = lib.mkOption {
type = lib.types.listOf lib.types.str;
example = [ "seaweedfs_filer_nix1:8888" "seaweedfs_filer_nix2:8888" ];
description = "List of filer addresses (hostname:port) to connect to.";
};
mountPoint = lib.mkOption {
type = lib.types.path;
default = "/mnt/service/seaweedfs";
description = "The absolute path where the SeaweedFS volume will be mounted.";
};
cacheDir = lib.mkOption {
type = lib.types.path;
default = "/data/seaweedfs/cache";
description = "Directory for local FUSE cache.";
};
volumeDir = lib.mkOption {
type = lib.types.path;
default = "/data/seaweedfs/volume";
description = "Directory for local volume.";
};
user = lib.mkOption {
type = lib.types.str;
default = "root";
description = "The user to run the mount process as.";
};
group = lib.mkOption {
type = lib.types.str;
default = "root";
description = "The group to run the mount process as.";
};
};
# This part of the module will be activated only if services.seaweedfs-mount.enable = true;
config = lib.mkIf cfg.enable {
# 1. Ensure the FUSE kernel module is loaded at boot.
boot.kernelModules = [ "fuse" ];
# 2. Ensure the seaweedfs package is available in the system path.
environment.systemPackages = [ pkgs.seaweedfs ];
# 3. Create the necessary directories before the service starts.
systemd.tmpfiles.rules = [
"d ${cfg.mountPoint} 0755 ${cfg.user} ${cfg.group} -"
"d ${cfg.cacheDir} 0755 ${cfg.user} ${cfg.group} -"
"d ${cfg.volumeDir} 0755 ${cfg.user} ${cfg.group} -"
];
# 4. Define the systemd service itself.
systemd.services.seaweedfs-mount = {
description = "SeaweedFS FUSE Mount Service";
# Start after the network is fully available.
wantedBy = [ "multi-user.target" ];
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
# Service configuration details.
serviceConfig = {
# The user and group to run the command as.
User = cfg.user;
Group = cfg.group;
# The core command to start the mount process.
# We build the -filer string by joining the list of addresses with commas.
ExecStart = ''
${pkgs.seaweedfs}/bin/weed mount \
-dir=${cfg.mountPoint} \
-cacheDir=${cfg.cacheDir} \
-filer=${lib.concatStringsSep "," cfg.filerAddresses} \
-allowOthers=true \
-map.uid="1000:0" \
-map.gid="1000:0" \
-dirAutoCreate \
-cacheCapacityMB=15000 \
-filer.path=/
'';
# Command to cleanly unmount on service stop.
ExecStop = "${pkgs.util-linux}/bin/umount ${cfg.mountPoint}";
# *** This is the key part for your request ***
# Always restart the service if it fails.
Restart = "always";
# Wait 10 seconds before attempting to restart.
RestartSec = "10s";
# Required for FUSE mounts to work correctly under systemd.
Type = "simple";
KillMode = "process";
};
};
};
}