Skip to content

Instantly share code, notes, and snippets.

@mwilsoncoding
Last active August 27, 2020 01:25
Show Gist options
  • Save mwilsoncoding/0b6d735956138e8ca7c40ab8bc09d3ee to your computer and use it in GitHub Desktop.
Save mwilsoncoding/0b6d735956138e8ca7c40ab8bc09d3ee to your computer and use it in GitHub Desktop.

Nix Tidbits

In which tidbits regarding nix can be found...

Nix

Inherit

# introduces the 'hi' attribute to the following scope
# only valid inside a let block
nix-repl> let inherit ({greeting = "helloworld";}) greeting; in greeting
"helloworld"

System nixpkgs

import <nixpkgs> {};

Git nixpkgs

let
  inherit (import <nixpkgs> {}) fetchFromGitHub:
  nixpkgs = fetchFromGitHub {
    owner = "NixOS";
    repo = "nixpkgs-channels";
    rev = "00abc...12300";
    sha256 = "345oeu...nth876";
  };
in ...

NixOS

Build a package from a channel

nix build -f channel:$CHANNEL hello

Install a package from a channel

nix-env -f https://github.com/NixOS/nixpkgs-channels/archive/${CHANNEL}.tar.gz -iA hello

Remove old generations

sudo nix-env -p /nix/var/nix/profiles/system --delete-generations old
nix-collect-garbage -d
nix-env -p /nix/var/nix/profiles/system --list-generations
## Remove entries from /boot/loader/entries:
find /boot/loader/entries ! -name nixos-generation-${CURRENT_GENERATION_NUMBER}.conf -type f -exec sudo rm {} +

NixOps

Deploy to GCP

  • create GCP account
  • create project foo
  • create service account bar
  • download a JSON key for bar locally and note its path
  • export these variables:
export GCE_PROJECT=foo
export GCE_SERVICE_ACCOUNT=bar@iam.gserviceaccount.com
export ACCESS_KEY_PATH=/path/to/key.json
  • write a nix expression containing your desired network
let baz = qux; in { network; }
  • nixops create -d foonet ./file.nix ./file2.nix
  • nixops deploy -d foonet
  • edit expressions as desired and then
    • nixops modify -d foonet ./file.nix ./file2.nix
    • nixops deploy -d foonet
  • if anything gets into a wonky state
    • nixops deploy -d foonet --check
  • if all else fails, turn it off and on again
    • nixops destroy -d foonet --confirm
    • nixops deploy -d foonet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment