Skip to content

Latest commit

 

History

History
139 lines (126 loc) · 3.7 KB

shell.org

File metadata and controls

139 lines (126 loc) · 3.7 KB

Nix-shell and nix develop

Table of Content

Terminal

Shell

  • Create a shell where a package is available.
    • $ nix-shell -p <packages>
  • Install a package and run command. Only temporarly enter shell.
    • $ nix-shell -p <package> --run <cmd> or –command
  • Install package in the current active $SHELL (current terminal)
    • $ nix shell nixpkgs#<package>
    • Since nixpkgs is used, you can also specify the branch.
      • $ nix shell nixpkgs/<branch>#<package>

Run

Experimental alternative

  • Installs the package and runs /bin .
  • $ nix run nixpkgs#<package>

Shell.nix

  • $ nix-shell will default to an existing file named shell.nix
  • to specify a specific nix-file use $ nix-shell <nix-file.nix>

Create shell.nix files

General

with import <nixpkgs> {};
mkShell {
  name = "shell name";
  buildInputs = with pkgs; [
    packages
  ];
}

Stable vs Unstable

let
  unstableTarball = fetchTarball https://github.com/NixOS/nixpkgs/arhive/nixos-unstable.tar.gz;
  stable = import <nixpkgs> {};
  unstable = import unstableTarball{};
in with import <nixpkgs> {};
stdenv.mkDerivation {
  name = "shell name";
  buildInputs = [
    unstable.hello
    stable.world
  ];
}

Insecure

with import <nixpkgs> {};
mkShell {
  name = "shell name";
  buildInputs = with pkgs; [];
  permittedInsecurePackages = with pkgs; [
    packages
  ];
}
  • In some cases this will still break the package. If the package has a flake.nix it might be better to:
    • $ NIXPKGS_ALLOW_INSECURE=1 nix run nixpkgs#etcher --impure

Appimages

  • This is mainly an issue for NixOS users.
  • Recommended solution: You can use the shell below or use a package called “appimage-run”. You can then $ appimage-run <appimage>.
let
  version = "number";
  buildnumber = "number"
in { pkgs ? import <nixpkgs> {} }:
pkgs.appimageTools.wrapType2 {
  name = "appimage-name"
  src = pkgs.fetchurl {
    url = "https://website.com/link/to/appimage-${version}-and-or-${buildnumber}.AppImage";
    sha256 = "0000000000000000000000000000000000000000000000000000";
  };
  # src = /home/matthias/app.AppImage
}
  • If you want to run the shell it is recommended to use $ nix build -f shell.nix.

Shell Hook

  • Initialisation: Commands to run after everything is sourced.
with import <nixpkgs> {};
stdenv.mkDerivation {
  name = "shell name";
  buildInputs = with pkgs; [];
  shellHook = ''
    echo "hello world"
  '';
}

Nix develop

Shell

  • Install package in the current active $SHELL (current terminal)
    • $ nix develop
  • If the flake has multiple hosts.
    • $ nix develop </path/to/flake.nix>#<host>

Create flake.nix

  • Using the nixpkgs-unstable branch, <packages> will be installed inside a shell. This host is default. You can create multiple hosts and rename them.
{
  description = "A development environment";
  inputs = {
    nixpkgs = { url = "github:nixos/nixpkgs/nixpkgs-unstable"; };
  };
  outputs = inputs:
    let
      pkgs = import inputs.nixpkgs { system = "x86_64-linux"; };
    in {
      # default host
      devShells.x86_64-linux.default = inputs.nixpkgs.legacyPackages.x86_64-linux.mkShell {
        buildInputs = [ <packages> ];
      };
    };
}

Remove shell packages

  • exit shell and just collect garbage like $ sudo nix-collect-garbage -d