Ok, turns out I was wrong. My previous one does not work. I had some other stuff going on in my configuration.nix that made that work until it didn’t. I was able to actually figure out how to get it to work. From what I have found the openiscsi nixpkg options are half baked.
According to the Arch Wiki, “To log in to a target during boot, [enable] iscsi.service and make sure the nodes have node.startup = automatic in their configuration (/var/lib/iscsi/nodes/iqn.node-name/node-ip-address,port).”
The issue with this is nix does not create a /var/lib/iscsi/nodes/. When you connect to target. There is the services.openiscsi.extraConfig but that appends lines to the /etc/iscsid.conf file which will not work.
To get around this I tried many things but the one that ended up working the best was created a systemd to connect to it. Here is everything you need to get it to work in your configuration.nix:
{
...
boot.kernelModules = [ "iscsi_tcp" ]; # not sure if needed but added just incase
services.openiscsi = {
enable = true; # Enable openiscsi daemon
name = "iqn.2024-09.com.nixos:my-nixos-initiator"; # Set **YOUR** iSCSI initiator name not the one that you are trying to connect to. I am pretty sure this can be whatever you want.
discoverPortal = "10.0.0.3"; # IP of your iscsi server
};
# Custom systemd service for logging in to a specific iSCSI target, you can name the service whatever youd like
systemd.services.iscsi-login-lingames = {
description = "Login to iSCSI target iqn.2005-10.org.freenas.ctl:lingames";
after = [ "network.target" "iscsid.service" ];
wants = [ "iscsid.service" ];
serviceConfig = {
ExecStartPre = "${pkgs.openiscsi}/bin/iscsiadm -m discovery -t sendtargets -p 10.0.0.3";
ExecStart = "${pkgs.openiscsi}/bin/iscsiadm -m node -T iqn.2005-10.org.freenas.ctl:lingames -p 10.0.0.3 --login";
ExecStop = "${pkgs.openiscsi}/bin/iscsiadm -m node -T iqn.2005-10.org.freenas.ctl:lingames -p 10.0.0.3 --logout";
Restart = "on-failure";
RemainAfterExit = true;
};
wantedBy = [ "multi-user.target" ];
};
fileSystems."<path-to-where-you-want-it-mounted>" = {
device = "/dev/disk/by-path/ip-10.0.0.3:3260-iscsi-iqn.2005-10.org.freenas.ctl:lingames-lun-0"; # Replace with the correct device path after iSCSI login you cloud also do /dev/disk/by-uuid/<UUID-of-device>
fsType = "ext4"; # Or the correct filesystem type
options = [ "_netdev" "nofail" ]; # Ensures network is up before mounting and wont fail to boot if it cant connect
};
...
}
This has been the only way I have found to get this to work if you want autologin at boot. There also is the services.openiscsi.enableAutoLoginOut which will log you into every available iscsi share it can find on the network. This could work if you don’t mind connecting to all of them and then you can just mount the one you want. I have many iscsi shares and that would have annoyed me. Hopefully we will get some better iscsi support in nixos. I would try but I am still learning nix.