nix-shell

nix-shell #

这只是个参考,应该转向用 nix develop.

http://ghedam.at/15978/an-introduction-to-nix-shell https://nicu.dev/posts/dirnev-nix-shell

nix-shell -p ripgrep
nix-shell -p ripgrep --run "which rg"
  • nix-shell [.nix], 默认先找 shell.nix, 然后是 default.nix
  • 需要得到一个 derivation。(derivation)[https://nixos.org/guides/nix-pills/our-first-derivation.html]
with (import <nixpkgs> {});
mkShell {
  shellHook = ''
    alias l="lsd"
    export FOO=bar
  '';
  buildInputs = [
    ripgrep
  ];
}
  • direnv 中使用
use nix

python nix-shell example #

# python.nix
with (import <nixpkgs> {});
let
  my-python-packages = python-packages:
    with python-packages; [
      pandas
      requests
      # other python packages you want
    ];
  python-with-my-packages = python3.withPackages my-python-packages;
in
mkShell {
  buildInputs = [
    python-with-my-packages
  ];
}

rust nix-shell example #

TODO: 下面的例子是不能运行的,需要纠正。

with (import <nixpkgs> {});
let src = fetchFromGitHub {
    owner = "mozilla";
    repo = "nixpkgs-mozilla";
    # rev = "9f35c4b09fd44a77227e79ff0c1b4b6a69dff533";
    # sha256 = "18h0nvh55b5an4gmlgfbvwbyqj91bklf1zymis6lbdh75571qaz0";
};
in
with import "${src.out}/rust-overlay.nix" pkgs pkgs;
stdenv.mkDerivation {
  name = "rust-env";
  buildInputs = [
    # Note: to use use stable, just replace `nightly` with `stable`
    latest.rustChannels.nightly.rust

    # Add some extra dependencies from `pkgs`
    pkgconfig openssl
  ];

  # Set Environment Variables
  RUST_BACKTRACE = 1;
}