1{ config, lib, pkgs, ... }: with lib;
2let
3 cfg = config.services.openiscsi;
4in
5{
6 options.services.openiscsi = with types; {
7 enable = mkEnableOption (lib.mdDoc "the openiscsi iscsi daemon");
8 enableAutoLoginOut = mkEnableOption (lib.mdDoc ''
9 automatic login and logout of all automatic targets.
10 You probably do not want this.
11 '');
12 discoverPortal = mkOption {
13 type = nullOr str;
14 default = null;
15 description = lib.mdDoc "Portal to discover targets on";
16 };
17 name = mkOption {
18 type = str;
19 description = lib.mdDoc "Name of this iscsi initiator";
20 example = "iqn.2020-08.org.linux-iscsi.initiatorhost:example";
21 };
22 package = mkOption {
23 type = package;
24 description = lib.mdDoc "openiscsi package to use";
25 default = pkgs.openiscsi;
26 defaultText = literalExpression "pkgs.openiscsi";
27 };
28
29 extraConfig = mkOption {
30 type = str;
31 default = "";
32 description = lib.mdDoc "Lines to append to default iscsid.conf";
33 };
34
35 extraConfigFile = mkOption {
36 description = lib.mdDoc ''
37 Append an additional file's contents to /etc/iscsid.conf. Use a non-store path
38 and store passwords in this file.
39 '';
40 default = null;
41 type = nullOr str;
42 };
43 };
44
45 config = mkIf cfg.enable {
46 environment.etc."iscsi/iscsid.conf.fragment".source = pkgs.runCommand "iscsid.conf" {} ''
47 cat "${cfg.package}/etc/iscsi/iscsid.conf" > $out
48 cat << 'EOF' >> $out
49 ${cfg.extraConfig}
50 ${optionalString cfg.enableAutoLoginOut "node.startup = automatic"}
51 EOF
52 '';
53 environment.etc."iscsi/initiatorname.iscsi".text = "InitiatorName=${cfg.name}";
54
55 system.activationScripts.iscsid = let
56 extraCfgDumper = optionalString (cfg.extraConfigFile != null) ''
57 if [ -f "${cfg.extraConfigFile}" ]; then
58 printf "\n# The following is from ${cfg.extraConfigFile}:\n"
59 cat "${cfg.extraConfigFile}"
60 else
61 echo "Warning: services.openiscsi.extraConfigFile ${cfg.extraConfigFile} does not exist!" >&2
62 fi
63 '';
64 in ''
65 (
66 cat ${config.environment.etc."iscsi/iscsid.conf.fragment".source}
67 ${extraCfgDumper}
68 ) > /etc/iscsi/iscsid.conf
69 '';
70
71 systemd.packages = [ cfg.package ];
72
73 systemd.services."iscsid".wantedBy = [ "multi-user.target" ];
74 systemd.sockets."iscsid".wantedBy = [ "sockets.target" ];
75
76 systemd.services."iscsi" = mkIf cfg.enableAutoLoginOut {
77 wantedBy = [ "remote-fs.target" ];
78 serviceConfig.ExecStartPre = mkIf (cfg.discoverPortal != null) "${cfg.package}/bin/iscsiadm --mode discoverydb --type sendtargets --portal ${escapeShellArg cfg.discoverPortal} --discover";
79 };
80
81 environment.systemPackages = [ cfg.package ];
82 boot.kernelModules = [ "iscsi_tcp" ];
83 };
84}