Skip to content

Instantly share code, notes, and snippets.

@JohnRTitor
Last active November 10, 2024 07:34
Show Gist options
  • Save JohnRTitor/4cfdc99c853f5a82e4f8ac5e3679fd7e to your computer and use it in GitHub Desktop.
Save JohnRTitor/4cfdc99c853f5a82e4f8ac5e3679fd7e to your computer and use it in GitHub Desktop.
Nix Tips and Tricks

Build normal packages - x64

# x64 packages or architechture
nix build ".#hello"

# 32bit x86 packages
nix build ".#pkgsi686Linux.hello"

# 32bit x86 drivers
nix build ".#driversi686Linux.hello"

Cross compile aarch64-linux binaries on x64-linux

nix build ".#legacyPackages.x86_64-linux.pkgsCross.aarch64-multiplatform.hello"

Compile aarch64-linux binaries (native) with binfmt

nix build ".#hello" --system "aarch64-linux"

Nix run/build with override

nix run --impure --expr '(builtins.getFlake "github:nixos/nixpkgs/nixpkgs-unstable").legacyPackages."${builtins.currentSystem}".discord.override {withVencord = true;}'

Build the NixOS manual

nix build --print-out-paths ".#htmlDocs.nixosManual.x86_64-linux"

Build from a rev, PR

nix build "github:nixos/nixpkgs/refs/pull/322591/merge#hello"

nix build "github:nixos/nixpkgs/refs/pull/322591/head#hello"

nix build "github:nixos/nixpkgs/b663d72f08ead954af19c42e18152186451a1666#hello"

Simple nix-repl with nixpkgs loaded

nix repl --expr 'import <nixpkgs>{}'

Override in cli

nix shell --expr "(import <nixpkgs>{}).python3Packages.python.withPackages (ps: [ ps.ipykernel ])" --impure

Run Tests

## Run package-internal tests
nix-build --attr pkgs.PACKAGE.passthru.tests

## Run NixOS tests
nix-build --attr nixosTests.NAME
nix build ".#nixosTests.NAME"

## Run Global package tests, where PACKAGE is the name of the test listed in pkgs/test/default.nix
nix-build --attr tests.PACKAGE
@JohnRTitor
Copy link
Author

In Nixpkgs you can use

  passthru.updateScript = unstableGitUpdater {
    url = "https://github.com/Mdashdotdashn/littlegptracker.git";
  };

to update packages that always need to be on the latest changes/commit.

@JohnRTitor
Copy link
Author

JohnRTitor commented May 13, 2024

Prefetch a git repository with a specific tag or commit

nix-prefetch-git [email protected]:ninja-build/ninja.git --rev v1.12.1

@JohnRTitor
Copy link
Author

JohnRTitor commented Jun 21, 2024

Disable pushing to a upstream repo where you have write access

git remote set-url upstream --push no_push

@JohnRTitor
Copy link
Author

JohnRTitor commented Jul 30, 2024

Fetch a patch and do stuff with it.
In this case a patch was fetched compressed in a .zst (zstd) archive, we decompress and print the patch after.

  kernelPatches = {
    name = "scx-${kernel.version}.patch";
    patch = fetchpatch2 {
      url = "https://github.com/sched-ext/scx-kernel-releases/releases/download/v6.10.1-scx1/linux-v6.10.1-scx1.patch.zst";
      decode = ''${zstd}/bin/zstd -d -'';
      hash = "sha256-1Bet+BJ6G1l5A2OEmDKnKJYtYF97wrEv/rDeh7Ny0ZM=";
    };
  }

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