1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.target;
7in
8{
9 ###### interface
10 options = {
11 services.target = with types; {
12 enable = mkEnableOption (lib.mdDoc "the kernel's LIO iscsi target");
13
14 config = mkOption {
15 type = attrs;
16 default = {};
17 description = lib.mdDoc ''
18 Content of /etc/target/saveconfig.json
19 This file is normally read and written by targetcli
20 '';
21 };
22 };
23 };
24
25 ###### implementation
26 config = mkIf cfg.enable {
27 environment.etc."target/saveconfig.json" = {
28 text = builtins.toJSON cfg.config;
29 mode = "0600";
30 };
31
32 environment.systemPackages = with pkgs; [ targetcli ];
33
34 boot.kernelModules = [ "configfs" "target_core_mod" "iscsi_target_mod" ];
35
36 systemd.services.iscsi-target = {
37 enable = true;
38 after = [ "network.target" "local-fs.target" ];
39 requires = [ "sys-kernel-config.mount" ];
40 wantedBy = [ "multi-user.target" ];
41 serviceConfig = {
42 Type = "oneshot";
43 ExecStart = "${pkgs.python3.pkgs.rtslib}/bin/targetctl restore";
44 ExecStop = "${pkgs.python3.pkgs.rtslib}/bin/targetctl clear";
45 RemainAfterExit = "yes";
46 };
47 };
48
49 systemd.tmpfiles.rules = [
50 "d /etc/target 0700 root root - -"
51 ];
52 };
53}