Usage of "writeTextFile" and "let" - noob question

Hello. I have a noob question regarding the NixOS language.
I want to add this to my nvidia_optimus.nix config:

let
  gnome-gpu-rule = pkgs.writeTextFile {
    name = "61-mutter-primary-gpu.rules";
    text = ''
      ENV{DEVNAME}=="/dev/dri/card1", TAG+="mutter-device-preferred-primary"
    '';
    destination = "/etc/udev/rules.d/61-mutter-primary-gpu.rules";
  };
in {
  imports =
    [ # Include the results of the hardware scan.
      ./hardware-configuration.nix
    ];
  ### rest of configuration.nix...
  services.udev.packages = [ gnome-gpu-rule ];
  ### rest of configuration.nix...
}

Source: Dual GPU setup: nvidia-smi shows gnome-shell and doesn't sleep - #5 by nrdxp

When I rebuild I get:

       error: syntax error, unexpected LET, expecting INHERIT
       at /nix/store/bqx4q87pdzaam55hca48cdk1jmh097a6-source/hardware/nvidia_optimus.nix:39:3:
           38|
           39|   let
             |   ^
           40|     gnome-gpu-rule = pkgs.writeTextFile {

My understanding of basic programming is very low. I understand some basic PowerShell scripting defining and calling objects etc, but this is beyond me.
Do you have a suggestion of what or where I can look to make it work?

nvidia_optimux.nix
{ config, lib, pkgs, ... }:
{

  # Enable OpenGL
  hardware.graphics = {
    enable = true;
  };

  environment.variables = {
    GSK_RENDERER = "ngl";
  };  

  services.xserver.videoDrivers = [
    "modesetting"  # example for Intel iGPU; use "amdgpu" here instead if your iGPU is AMD
    "nvidia"
  ];

  hardware.nvidia = {
    # Modesetting is required for offloadning to work. Check nvidia-smi.
    modesetting.enable = true;
    open = lib.mkForce true;
    powerManagement.finegrained = true;    

    prime = {
      offload = {
        enable = true;
        enableOffloadCmd = true;
      };

      intelBusId = "PCI:0:2:0";
      nvidiaBusId = "PCI:1:0:0";
    
    };
    
    # Optionally, you may need to select the appropriate driver version for your specific GPU.
    package = config.boot.kernelPackages.nvidiaPackages.beta;
  };

  let
    gnome-gpu-rule = pkgs.writeTextFile {
      name = "61-mutter-primary-gpu.rules";
      text = ''
        ENV{DEVNAME}=="/dev/dri/card2", TAG+="mutter-device-preferred-primary"
      '';
      destination = "/etc/udev/rules.d/61-mutter-primary-gpu.rules";
    };
  in {
    imports =
      [ # Include the results of the hardware scan.
        ./hardware-configuration.nix
      ];
    ### rest of configuration.nix...
    services.udev.packages = [ gnome-gpu-rule ];
    ### rest of configuration.nix...
  }


}

let can’t be inside an attribute set, you can either move let ... in to before the attribute set begins (after { config, lib, pkgs, … }:) or (in this case) between services.udev.packages = and [.

Please add triple backticks (“```”) before and after your code so it renders properly like this:

{pkgs, ...}:

{
  services.foo.package = pkgs.bar;
}

You’re also not meant to add extra curly braces. Every module is an attrset, use the braces that are already there.

Thank you for your input. I managed to get the rebuild running with this:

nvidia_optimus.nix
{ config, lib, pkgs, ... }:

let
  gnome-gpu-rule = pkgs.writeTextFile {
    name = "61-mutter-primary-gpu.rules";
    text = ''
      ENV{DEVNAME}=="/dev/dri/card2", TAG+="mutter-device-preferred-primary"
    '';
    destination = "/etc/udev/rules.d/61-mutter-primary-gpu.rules";
  };
in 

{
   # Enable OpenGL
   hardware.graphics = {
     enable = true;
   };

   environment.variables = {
     GSK_RENDERER = "ngl";
   };  

   services.xserver.videoDrivers = [
     "modesetting"  # example for Intel iGPU; use "amdgpu" here instead if your iGPU is AMD
     "nvidia"
   ];

   hardware.nvidia = {
     # Modesetting is required for offloadning to work. Check nvidia-smi.
     modesetting.enable = true;
     open = lib.mkForce true;
     powerManagement.finegrained = true;    
     package = config.boot.kernelPackages.nvidiaPackages.beta;
     prime = {
       offload = {
           enable = true;
           enableOffloadCmd = true;
         };    
         intelBusId = "PCI:0:2:0";
         nvidiaBusId = "PCI:1:0:0";
       };
     };
    
   services.udev.packages = [ gnome-gpu-rule ];
}