"loop" through list instead of repeating

Given a list:

sites = [
    "siteone.com"
    "sitetwo.com"
];

how would I “loop” through them, instead of writing this code twice (it’s actually multiple times…) I have several other parts to this, but for simplicity, I’d like to see how to “repeat” this step… ${site} is supposed to be an item in the sites list.

    services.caddy.virtualHosts."https://${site}:443".extraConfig = ''
      root * ${webPath}/${site}
      php_fastcgi unix/${config.services.phpfpm.pools."${site}".socket}
      file_server
    '';

Have a look at map and builtins.listToAttrs.

Thanks, I did have a look and saw this post:

However, I’m not able to replicate it… I undid my attempt and continued working. I’m happy to try again and share what I have. Though it just didn’t work at all… I’m too green for this :sweat_smile:

progressing to the next step i see… :grin:

aaron@framework ~> nix repl -f ~/nixpkgs
Welcome to Nix 2.18.2. Type :? for help.

warning: Nix search path entry '/nix/var/nix/profiles/per-user/root/channels' does not exist, ignoring
Loading installable ''...
Added 21796 variables.
nix-repl> sites = [ "siteone.com" "sitetwo.com" ]

nix-repl> :p lib.listToAttrs (map (site: lib.nameValuePair "https://${site}:443" { extraConfig = ''
          root * /var/www/${site}
          ''; }) sites)
{ "https://siteone.com:443" = { extraConfig = "root * /var/www/siteone.com\n"; }; "https://sitetwo.com:443" = { extraConfig = "root * /var/www/sitetwo.com\n"; }; }

if you’re coming from php background then maybe functional programming is new to you… don’t worry, it’ll make more sense soon enough :wink: soon you’ll be hooked :heart:

2 Likes

:sweat_smile: Yeah, was taking a break from trying to figure out why I can’t use the nfs4 mount in phpfpm.

Now with this I am getting this error why adding the code you suggested:

       error: syntax error, unexpected ':'

       at /nix/store/kj6rpgij0n6nwg7m6gsd6lild06nhip6-source/modules/lamp.nix:90:3:

           89|
           90|   :p lib.listToAttrs (map (site: lib.nameValuePair "https://${site}:443" { extraConfig = ''
             |   ^
           91|           root * /var/www/${site}

I really appreciate your help and kind words! This is certainly unexplored territory for me. I’m going to keep trying to configure my system and learn as I go.

Certainly looking forward to getting a handle on this new (to me) language.

sorry, that code was specifically in the nix repl command, useful for learning nix language and library/builtins… you should definitely try it out!

to adapt this to your scenario in your lamp.nix file something like this should do it:

    services.caddy.virtualHosts = lib.listToAttrs (map (site: lib.nameValuePair "https://${site}:443" {
      extraConfig = ''
        root * ${webPath}/${site}
        php_fastcgi unix/${config.services.phpfpm.pools."${site}".socket}
        file_server
      ''; }) sites)

I’ll look into it for sure! Looks very powerful, as most Nix related things do.

I think where I went wrong here was trying to wrap this code so it would work on all sets, instead of per-set. Thanks!!!