Skip to content

Instantly share code, notes, and snippets.

@phdoerfler
Last active April 20, 2021 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phdoerfler/65a2da7cd30c87632367f7b4e58ba5dc to your computer and use it in GitHub Desktop.
Save phdoerfler/65a2da7cd30c87632367f7b4e58ba5dc to your computer and use it in GitHub Desktop.
nixos

NixOS

Path to $binary/$derivation

$derivation/bin gets linked into PATH only when explicitely installing it. If, e.g., sieve-filter is only in /nix/store because something else dependet on dovecot_pigeonhole, it won't be in PATH.

Path to $derivation Part 2

$ nix repl
nix-repl> let pkgs = import <nixos> {}; in "${pkgs.openssh}"
"/nix/store/xig1gasyh9m5fvgk676ab2qa9hbwgcjk-openssh-7.5p1"

Shell Script

/etc/nixos/something.nix:

{ pkgs, ... }:

{
  environment.systemPackages = [ (pkgs.writeShellScriptBin "hello-i-am-a-custom-script" ''
    set -e


  '') ];
}

Systemd service

systemd service

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

{
  systemd.services.thermonitor = {
    description = "Hello I am your friendly neighborhood daemon";
    serviceConfig = {
      ExecStart = "/home/youruser/thermonitor.py";
      User = "youruser";
      Group = "users";
      WorkingDirectory = "/home/youruser";
    };
    wantedBy = [ "multi-user.target" ];
    after = [ "network.target" ];
    path = [ pkgs.python3 pkgs.moreutils pkgs.netcat ];
  };
}

/usr/bin/env: ‘sh’: No such file or directory

path = [ pkgs.bash ];

Oh shit

Profiles

sudo nixos-rebuild switch --rollback

sudo nix-env --list-generations --profile /nix/var/nix/profiles/system

Siehe: List-generations and rollback to any configuration · Issue #24374

Misc

nix-env

nix-env -f "<nixpkgs>" -qaP -A haskellPackages

show option's current value and documentation

nixos-option <option>

runCommand

runCommand "name" { buildInputs = [ foo ]; }; ''some bash code'';

pkgs.runCommand "configtest" { buildInputs = [rsnapshot]; }; ''rsnapshot configtest'';

For this to happen after generating the config, you have to be clever with referencing: You want nix to first build the config and then run runCommand afterwards.

Since the config needs to be copied into the directory where runCommand runs, this works in this case.

nix-shell haskell

#! /usr/bin/env nix-shell
#! nix-shell -i runghc -p haskellPackages.ghc haskellPackages.HTTP

import Network.HTTP

main = do
  resp <- Network.HTTP.simpleHTTP (getRequest "http://nixos.org/")
  body <- getResponseBody resp
  print (take 100 body)

Compiled Languages as Scripts

See nix-script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment