Configure iscsi

Nix noob here. I am trying to figure out how to configure my configuration.nix to connect to an iscsi share with my steam library and auto mount at boot. This is what I have so far in my configuration.nix:

services.openiscsi = {
    enable = true;
    discoverPortal = "10.0.0.3:3260";
    name = "iqn.2005-10.org.freenas.ctl:lingames";
    enableAutoLoginOut = true;

  environment.systemPackages = with pkgs; [
    openiscsi
  ];

Then inside my hardware-configuration.nix

fileSystems."/home/<username>/Games" = {
    device = "/dev/disk/by-uuid/<uuid of disk>";
    fsType = "btrfs";
    options = [ "nofail" "_netdev" "auto" "exec" "defaults"];
};

My issues is the only way I could find how to auto connect to the share was the “enableAutoLoginOut” but that logs into all iscsi shares on the discoverPortal which I do not want to do as I have many. It also does not survive reboots until I do another ‘nixos-rebuild switch’. I have looked at all the documention (of which there is no a lot) nor could I figure out any of the options based on the nixpkgs for openiscsi to figure out how to just connect to that one iscsi share and mount it at boot. If anyone has a solution or pointers, that would be great. This is the last thing that is stopping me from fully switching from Arch where I can do this no problem.

What about services.openiscsi.extraConfig? Perhaps that can be used to specify shares?

You were right, thank you. I was able to figure out what to add to the extraConfig option with a little help form the arch wiki.

{
  services.openiscsi = {
    enable = true;
    discoverPortals = [ "10.0.0.3:3260" ];
    targets = [ "iqn.2005-10.org.freenas.ctl:lingames" ];
    extraConfig = ''
      node.startup = automatic
      node.session.auth.authmethod = None
    '';
  };

Ok, turns out I was wrong. That does not work. I had some other stuff going on in my configuration.nix that made that work. I was able to actually figure out how to get it to work. From what I have found the openiscsi nixpkg is 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 creating a systemd service 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.

Hope this helps someone else.