Skip to content

Instantly share code, notes, and snippets.

@MarWeUMR
Last active April 9, 2024 14:35
Show Gist options
  • Save MarWeUMR/b60869b8b57f7146ad7f6f60039e8f62 to your computer and use it in GitHub Desktop.
Save MarWeUMR/b60869b8b57f7146ad7f6f60039e8f62 to your computer and use it in GitHub Desktop.
Nix Cheat Sheet

How to increase API rate-limit with GitHub?

Just put this in /etc/nix/nix.conf with a personal access token.

access-tokens = github.com=<TOKEN>

How to provide a binary under a different name?

packages = with pkgs;
          [
            google-chrome
            # chrome would be executed as `google-chrome-stable`
            # with the below command, a `google-chrome` executable is created.
            # usefull with e.g. selenium and friends
            (pkgs.writeShellScriptBin "google-chrome" "exec -a $0 ${google-chrome}/bin/google-chrome-stable $@")
          ]
          ...

How to use a pip package, that is not available in the nix store?

{
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
  inputs.flake-utils.url = "github:numtide/flake-utils";
  outputs = {
    self,
    nixpkgs,
    flake-utils,
  }:
    flake-utils.lib.eachDefaultSystem (system: let
      pkgs = import nixpkgs {
        system = system;
      };

      webdriver = pkgs.python310Packages.buildPythonPackage rec {
        name = "webdriver_manager";
        version = "v4.0.1";

        src = pkgs.fetchFromGitHub {
          owner = "SergeyPirogov";
          repo = "${name}";
          rev = "${version}";
          sha256 = "sha256-PdUlloJ4DncnktKQHofn/OLVrgSVyWhaeEEhl3Hgjek";
        };

        doCheck = false;
        BuildInputs = [pkgs.python310Packages.pip];
      };

      wire = pkgs.python310Packages.buildPythonPackage rec {
        name = "selenium-wire";
        version = "master";

        src = pkgs.fetchFromGitHub {
          owner = "wkeeling";
          repo = "${name}";
          rev = "${version}";
          sha256 = "sha256-v6WFqQUmFmRqI7lBqu40R8qHtD3aqU7K7lo+52uN8rU";
        };

        doCheck = false;
        BuildInputs = [pkgs.python310Packages.pip];
      };
    in {
      devShells.default = pkgs.mkShell {
        packages = with pkgs;
          [
            python310
            virtualenv
            webdriver # not in store
            wire # not in store
          ] ++ (with pkgs.python310Packages; [
            pip
            pymongo
            requests
            selenium
            beautifulsoup4
            # ... more pip packages from the nix store
          ]);
          );
      };
    });
}

Alternatives

{
  description = "Python venv development template";

  inputs = {
    utils.url = "github:numtide/flake-utils";
  };

  outputs = {
    nixpkgs,
    utils,
    ...
  }:
    utils.lib.eachDefaultSystem (system: let
      pkgs = import nixpkgs {inherit system;};

      # choose your python version here
      pythonPackages = pkgs.python311Packages;

      #---------------------------------------------------------------------------
      #
      # Here we build a pip package from github which is neither in the nix store,
      # nor up to date with pip/pypi
      #
      #---------------------------------------------------------------------------

      nipyapiCustom = pythonPackages.buildPythonPackage rec {
        name = "nipyapi";
        version = "8a2aec3ba7c84ccd83ae17462e8cdbb6ec4472ea";

        src = pkgs.fetchFromGitHub {
          owner = "Chaffelson";
          repo = "${name}";
          rev = "${version}";
          sha256 = "sha256-w56paJhc9qzlmGD2JtpoEzsHysByPaUmkgDcP/1kKyI=";
        };
        doCheck = false;
      };
    in {
      devShells.default = pkgs.mkShell {
        buildInputs = with pkgs; [
          # define your normal packages
          lnav
          jq

          # add your python/pip packages
          (python311.withPackages (ps: [
            ps.pip
            ps.requests

            # custom derived pip packages also go here
            nipyapiCustom
          ]))
        ];

        shellHook = ''
          # add shell specifics like environment variables
          # ...
        '';
      };
    });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment