Integrate shell commands in flake

Bonjour, :wave:

I begin in wonderful world of Nix! :innocent:

I’m happy to create and use a flake that I use without problem. But, now, I want to integrate a simple thing. Create a python venv with shell commands. But I’m stuck…

It’s part of my flake that works:

{
  description = "Fred Darwin system flake";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
    nix-darwin.url = "github:LnL7/nix-darwin";
    nix-darwin.inputs.nixpkgs.follows = "nixpkgs";
    nix-homebrew.url = "github:zhaofengli-wip/nix-homebrew";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = inputs@{ self, nix-darwin, nixpkgs, nix-homebrew, flake-utils }:
  let
    configuration = { pkgs, config, ... }: {

      nixpkgs.config.allowUnfree = true;

      environment.systemPackages =
        [
          pkgs.audacity
[..]
          pkgs.vim
        ];
        };

      onActivation.autoUpdate = true;
      onActivation.upgrade = true;

      };

      system.activationScripts.applications.text = let
        env = pkgs.buildEnv {
          name = "system-applications";
          paths = config.environment.systemPackages;
          pathsToLink = "/Applications";
        };
      in
        pkgs.lib.mkForce ''
        # Set up applications.
        echo "setting up /Applications..." >&2
        rm -rf /Applications/Nix\ Apps
        mkdir -p /Applications/Nix\ Apps
        find ${env}/Applications -maxdepth 1 -type l -exec readlink '{}' + |
        while read src; do
          app_name=$(basename "$src")
          echo "copying $src" >&2
          ${pkgs.mkalias}/bin/mkalias "$src" "/Applications/Nix Apps/$app_name"
        done
            '';

      system.defaults = {
          dock.autohide = true;
          dock.orientation = "bottom";
          dock.magnification = false;
          dock.mineffect = "genie";
          dock.persistent-apps = [
            "/System/Applications/Launchpad.app"
            "/Applications/Firefox.app"
[...]
            "/System/Applications/Utilities/Screenshot.app"
          ];
          dock.tilesize = 30;
          finder.FXPreferredViewStyle = "clmv";
          finder.AppleShowAllExtensions = true;
[...]
          trackpad.TrackpadThreeFingerTapGesture = 2;
          };

      # Auto upgrade nix package and the daemon service.
      services.nix-daemon.enable = true;
      # nix.package = pkgs.nix;

      # Necessary for using flakes on this system.
      nix.settings.experimental-features = "nix-command flakes";

      # Create /etc/zshrc that loads the nix-darwin environment.
      programs.zsh.enable = true;  # default shell on catalina
      # programs.fish.enable = true;

      # Set Git commit hash for darwin-version.
      system.configurationRevision = self.rev or self.dirtyRev or null;

      # Used for backwards compatibility, please read the changelog before changing.
      # $ darwin-rebuild changelog
      system.stateVersion = 5;

      # The platform the configuration will be used on.
      nixpkgs.hostPlatform = "aarch64-darwin";
    };
  in
  {
    darwinConfigurations."Macbook-Fred" = nix-darwin.lib.darwinSystem {
      modules = [
        configuration
        nix-homebrew.darwinModules.nix-homebrew
        {
          nix-homebrew = {
            # Install Homebrew under the default prefix
            enable = true;

            # Apple Silicon Only: Also install Homebrew under the default Intel prefix for Rosetta 2
            enableRosetta = true;

            # User owning the Homebrew prefix
            user = "fred";

            # Automatically migrate existing Homebrew installations
            autoMigrate = true;
          };
        }
      ];
    };

    # Expose the package set, including overlays, for convenience.
    darwinPackages = self.darwinConfigurations."Macbook-Fred".pkgs;
  };
}

And I would like to integrate, something simple, create a venv with shell command:

    flake-utils.lib.eachDefaultSystem (system: let
      pkgs = import nixpkgs {inherit system;};
    in {
      devShell = pkgs.mkShell {
        buildInputs = with pkgs; [
          python311
          python311Packages.virtualenv
        ];
        shellHook = ''
          python3 -m venv /Users/fred/venv/default
        '';
      };
    });

I tried to put in many locations in flake.

Before:

    flake-utils.lib.eachDefaultSystem (system: let
      pkgs = import nixpkgs {inherit system;};
    in {
      devShell = pkgs.mkShell {
        buildInputs = with pkgs; [
          python311
          python311Packages.virtualenv
        ];
        shellHook = ''
          python3 -m venv /Users/fred/venv/default
        '';
      };
    });

     system.activationScripts.applications.text = let
        env = pkgs.buildEnv {
          name = "system-applications";
          paths = config.environment.systemPackages;
          pathsToLink = "/Applications";
[...]

But I get this error:

 darwin-rebuild switch --flake .#Macbook-Fred
error: syntax error, unexpected '(', expecting '.' or '='
       at /nix/store/76452zsv9iv89hl07aqinbd2l9ghrpml-source/flake.nix:106:41:
          105|
          106|       flake-utils.lib.eachDefaultSystem (system: let
             |                                         ^
          107|         pkgs = import nixpkgs {inherit system;};

I tried:

      flake-utils.lib.eachDefaultSystem = let
        pkgs = import nixpkgs ;
      in {
        devShell = pkgs.mkShell {
          buildInputs = with pkgs; [
            python311
            python311Packages.virtualenv
          ];
          shellHook = ''
            python3 -m venv /Users/fred/venv/default
          '';
        };
      };

Error:

darwin-rebuild switch --flake .#Macbook-Fred
building the system configuration...
error:
       … while evaluating the attribute 'config.system.build.toplevel'
         at /nix/store/69bhrhj8imx84l6zvdz0zmh3qc8ykwvd-source/lib/modules.nix:334:9:
          333|         options = checked options;
          334|         config = checked (removeAttrs config [ "_module" ]);
             |         ^
          335|         _module = checked (config._module);

       … while calling the 'seq' builtin
         at /nix/store/69bhrhj8imx84l6zvdz0zmh3qc8ykwvd-source/lib/modules.nix:334:18:
          333|         options = checked options;
          334|         config = checked (removeAttrs config [ "_module" ]);
             |                  ^
          335|         _module = checked (config._module);

       … while evaluating the error message for definitions for `flake-utils', which is an option that does not exist

       … while evaluating a definition from `<unknown-file>'

       … while evaluating an attribute `lib`

       … while evaluating an attribute `eachDefaultSystem`

       … while evaluating an attribute `devShell`

       (stack trace truncated; use '--show-trace' to show the full, detailed trace)

       error: expected a set but found a function: «lambda @ /nix/store/69bhrhj8imx84l6zvdz0zmh3qc8ykwvd-source/pkgs/top-level/impure.nix:14:1»

I’m sorry… I don’t understand… :persevere:

Help ! :ring_buoy: