1 { config, lib, pkgs, ... }: with lib;
3 cfg = config.services.openiscsi;
6 options.services.openiscsi = with types; {
7 enable = mkEnableOption "the openiscsi iscsi daemon";
8 enableAutoLoginOut = mkEnableOption ''
9 automatic login and logout of all automatic targets.
10 You probably do not want this
12 discoverPortal = mkOption {
15 description = "Portal to discover targets on";
19 description = "Name of this iscsi initiator";
20 example = "iqn.2020-08.org.linux-iscsi.initiatorhost:example";
22 package = mkPackageOption pkgs "openiscsi" { };
24 extraConfig = mkOption {
27 description = "Lines to append to default iscsid.conf";
30 extraConfigFile = mkOption {
32 Append an additional file's contents to /etc/iscsid.conf. Use a non-store path
33 and store passwords in this file.
40 config = mkIf cfg.enable {
41 environment.etc."iscsi/iscsid.conf.fragment".source = pkgs.runCommand "iscsid.conf" {} ''
42 cat "${cfg.package}/etc/iscsi/iscsid.conf" > $out
45 ${optionalString cfg.enableAutoLoginOut "node.startup = automatic"}
48 environment.etc."iscsi/initiatorname.iscsi".text = "InitiatorName=${cfg.name}";
50 systemd.packages = [ cfg.package ];
52 systemd.services."iscsid" = {
53 wantedBy = [ "multi-user.target" ];
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}"
61 echo "Warning: services.openiscsi.extraConfigFile ${cfg.extraConfigFile} does not exist!" >&2
66 cat ${config.environment.etc."iscsi/iscsid.conf.fragment".source}
68 ) > /etc/iscsi/iscsid.conf
71 systemd.sockets."iscsid".wantedBy = [ "sockets.target" ];
73 systemd.services."iscsi" = mkIf cfg.enableAutoLoginOut {
74 wantedBy = [ "remote-fs.target" ];
75 serviceConfig.ExecStartPre = mkIf (cfg.discoverPortal != null) "${cfg.package}/bin/iscsiadm --mode discoverydb --type sendtargets --portal ${escapeShellArg cfg.discoverPortal} --discover";
78 environment.systemPackages = [ cfg.package ];
79 boot.kernelModules = [ "iscsi_tcp" ];