Problems with getting Python module "lichess" from PyPi

I’ve been trying for several days to get the module ( lichess · PyPI ) downloaded from my configuration.nix. I’ve been following the documentation from Python - NixOS Wiki and entries from the forum but I didn’t manage to get a working solution.

Here’s the error message:

[root@nixosl1:/etc/nixos]# nixos-rebuild switch --upgrade
unpacking channels...
error: syntax error, unexpected LET

       at /etc/nixos/configuration.nix:62:1:

           61| ###
           62| let
             | ^
           63| my-python-packages = ps: with ps; [

… and the code:

let
my-python-packages = ps: with ps; [
  # Lichess
  (
    buildPythonPackage rec {
      pname = "lichess";
      version = "0.2.8";
      src = fetchPypi {
        inherit pname version;
        sha256 = "2c5d4ad2d18978ee684c5e19368efcffa31b7884066f802859eb1fbeaece527c";
      };
      doCheck = false;
    }
  )
];
in

  # List packages installed in system profile. To search, run:
  # $ nix search wget
  # environment.systemPackages = with pkgs; [
  #   wget vim
  # ];
  environment.systemPackages = with pkgs; [
    ...
    (python3.withPackages (ps: with ps; [ requests my-python-packages ]))
    ...

I hope that someone can assist me. Many thanks in advance!

You can’t put a let-binding “as a name” inside an attrset. They only can be in places where nix values could be, for examlpe like:

{ 
....
  # List packages installed in system profile. To search, run:
  # $ nix search wget
  # environment.systemPackages = with pkgs; [
  #   wget vim
  # ];
  environment.systemPackages = with pkgs;
  let
    my-python-packages = ps: with ps; [
      # Lichess
      (
        buildPythonPackage rec {
          pname = "lichess";
          version = "0.2.8";
          src = fetchPypi {
            inherit pname version;
            sha256 = "2c5d4ad2d18978ee684c5e19368efcffa31b7884066f802859eb1fbeaece527c";
          };
          doCheck = false;
        }
      )
    ];
  in
  [
    ...
    (python3.withPackages (ps: with ps; [ requests my-python-packages ]))
    ...
  ];
...
}

Many many thanks for the hint; with 2 tries I managed to get nixos-rebuild running. But unfortunately it doesn’t produce the module:

[user@nixosl1:~]$ python
Python 3.10.11 (main, Apr  4 2023, 22:10:32) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> import lichess
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'lichess'

Any ideas?