Initial commit

This commit is contained in:
Patrick MARIE 2024-03-17 17:33:12 +01:00
commit c56d09816f
24 changed files with 564 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
result

66
README.md Normal file
View File

@ -0,0 +1,66 @@
# Nix samples
## Existing samples
* hello-world: Sample local C program;
* hello-world-github: Some github hosted program, built against Wayland;
* hello-world-callpackage: A basic callPackage usage;
* hello-world-go: Basic usage of buildGoModule;
* hello-world-docker: A docker image of a binary extracted from another derivation;
* simple-shell: Some shells;
* hello-versions: Choosing some package version;
* samples: A directory with found exicting recipes!
## Building, running, deleting
### hello-world
```sh
> cd /path/to/nix-samples
> nix-build hello-world/default.nix
this derivation will be built:
/nix/store/rwi8d1fv4hnpl194pi2cj7ikph2k5ny8-hello.drv
building '/nix/store/rwi8d1fv4hnpl194pi2cj7ikph2k5ny8-hello.drv'...
Running phase: unpackPhase
unpacking source archive /nix/store/29265kg6m3mzgf7rhfqjx34dwhgn9p4g-src
...
stripping (with command strip and flags -S -p) in /nix/store/4nzxqrm1gddddwlz55c2q398p0si4qhf-hello/bin
/nix/store/4nzxqrm1gddddwlz55c2q398p0si4qhf-hello
> nix profile install /nix/store/4nzxqrm1gddddwlz55c2q398p0si4qhf-hello
> nix profile list
Name: hello
Store paths: /nix/store/4nzxqrm1gddddwlz55c2q398p0si4qhf-hello
> hello
/home/mycroft/.nix-profile/bin/hello
```
Note: `~/.nix-profile/bin` was added in $PATH.
Prior to remove it, it must be unlinked from the profile and the build:
```sh
> nix-store --query --roots /nix/store/4nzxqrm1gddddwlz55c2q398p0si4qhf-hello
/home/mycroft/.local/state/nix/profiles/profile-7-link -> /nix/store/53zlkdqd1m2wdgi2a9q390ld9gkkkydf-profile
/home/mycroft/dev/nix-samples/result -> /nix/store/4nzxqrm1gddddwlz55c2q398p0si4qhf-hello
> nix profile remove /nix/store/4nzxqrm1gddddwlz55c2q398p0si4qhf-hello
removing 'hello'
>
```
It will be needed to wipe the profile history and the `result` link to be able to `nix-store --delete` or `nix-collect-garbage`.
## Resources
* callPackage: https://summer.nixos.org/blog/callpackage-a-tool-for-the-lazy/
* dockerfiles:
* https://nix.dev/tutorials/nixos/building-and-running-docker-images.html
* https://mitchellh.com/writing/nix-with-dockerfiles
* https://jameswillia.ms/posts/go-nix-containers.html

5
TODO.md Normal file
View File

@ -0,0 +1,5 @@
# Todo
* Fix hello-derivation: it tries to build hello with nobody:nogroup permissions.
* Adding a nix-build with bazel (sample2 from test-nix-bazel);
* Adding flake builds (including a build with bazel - sample3);

101
flake-basic/README.md Normal file
View File

@ -0,0 +1,101 @@
# flakes
Do not forget to `git add` the `flake.nix` file! Or you'll face the `does not exist` error.
```sh
> nix flake check
> nix build
> nix flake show
```
## Simplying
First step:
```nix
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};
outputs = inputs: {
packages."x86_64-linux".default = derivation {
name = "simple";
builder = "${inputs.nixpkgs.legacyPackages."x86_64-linux".bash}/bin/bash";
args = [ "-c" "echo foo > $out" ];
src = ./.;
system = "x86_64-linux";
};
};
}
```
Second step:
```nix
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};
outputs = { self, nixpkgs }: {
packages."x86_64-linux".default = derivation {
name = "simple";
builder = "${nixpkgs.legacyPackages."x86_64-linux".bash}/bin/bash";
args = [ "-c" "echo foo > $out" ];
src = ./.;
system = "x86_64-linux";
};
};
}
```
Third:
```nix
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};
outputs = { self, nixpkgs }:
let
system = "x86_64-linux";
in
{
packages.${system}.default = derivation {
name = "simple";
builder = "${nixpkgs.legacyPackages.${system}.bash}/bin/bash";
args = [ "-c" "echo foo > $out" ];
src = ./.;
system = system;
};
};
}
```
Fourth:
```nix
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};
outputs = { self, nixpkgs }:
let
name = "simple";
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
src = ./.;
in
{
packages.${system}.default = derivation {
inherit name src system;
builder = with pkgs; "${bash}/bin/bash";
args = [ "-c" "echo foo > $out" ];
};
};
}
```

27
flake-basic/flake.lock Normal file
View File

@ -0,0 +1,27 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1710631334,
"narHash": "sha256-rL5LSYd85kplL5othxK5lmAtjyMOBg390sGBTb3LRMM=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "c75037bbf9093a2acb617804ee46320d6d1fea5a",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

19
flake-basic/flake.nix Normal file
View File

@ -0,0 +1,19 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};
outputs = { self, nixpkgs }:
let
name = "simple";
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
src = ./.;
in
{
packages.${system}.default = derivation {
inherit name src system;
builder = with pkgs; "${bash}/bin/bash";
args = [ "-c" "echo foo > $out" ];
};
};
}

View File

@ -0,0 +1,19 @@
# flake-multi-platform
```
> nix build
...
> nix flake show
warning: Git tree '/home/mycroft/dev/nix-samples' is dirty
git+file:///home/mycroft/dev/nix-samples?dir=flake-multi-platform
└───packages
├───aarch64-darwin
│ └───default omitted (use '--all-systems' to show)
├───aarch64-linux
│ └───default omitted (use '--all-systems' to show)
├───x86_64-darwin
│ └───default omitted (use '--all-systems' to show)
└───x86_64-linux
└───default: package 'simple'
```

View File

@ -0,0 +1,61 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1710146030,
"narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1710631334,
"narHash": "sha256-rL5LSYd85kplL5othxK5lmAtjyMOBg390sGBTb3LRMM=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "c75037bbf9093a2acb617804ee46320d6d1fea5a",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

View File

@ -0,0 +1,25 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
# no need to define `system` anymore
name = "simple";
src = ./.;
pkgs = nixpkgs.legacyPackages.${system};
in
{
# `eachDefaultSystem` transforms the input, our output set
# now simply has `packages.default` which gets turned into
# `packages.${system}.default` (for each system)
packages.default = derivation {
inherit system name src;
builder = with pkgs; "${bash}/bin/bash";
args = [ "-c" "echo foo > $out" ];
};
}
);
}

View File

@ -0,0 +1,16 @@
let
nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-23.05";
pkgs = import nixpkgs { config = {}; overlays = []; };
in
derivation {
name = "hello";
builder = "${pkgs.bash}/bin/bash";
args = [ ./hello_builder.sh ];
inherit (pkgs) gnutar gzip gnumake gcc coreutils gawk gnused gnugrep;
bintools = pkgs.binutils.bintools;
src = pkgs.fetchzip {
url = "https://ftp.gnu.org/gnu/hello/hello-2.12.1.tar.gz";
sha256 = "sha256-1kJjhtlsAkpNB7f6tZEs+dbKd8z7KoNHyDHEJ0tmhnc=";
};
system = builtins.currentSystem;
}

View File

@ -0,0 +1,12 @@
#!/bin/bash
export PATH="$gnutar/bin:$gcc/bin:$gnumake/bin:$coreutils/bin:$gawk/bin:$gzip/bin:$gnugrep/bin:$gnused/bin:$bintools/bin"
echo $src
# I guess changing nixos version will change behavior of how fetchzip or something.
# This script differs from the version used against <nixpkgs>.
cd $src
ls -l
./configure --prefix=$out
make
make install

16
hello-versions/shell.nix Normal file
View File

@ -0,0 +1,16 @@
# https://lazamar.co.uk/nix-versions/
# The following will:
# - load <nixpkgs> to get the pkgs.mkShell lambda
# - load the given tarball (zsh 5.4.2 in channel unstable)
# - start a shell with this old version of zsh
{
oldpkgs ? (import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/473b58f20e491312a03c9c1578c89116e39a1d50.tar.gz") {}),
pkgs ? (import <nixpkgs> {})
}:
pkgs.mkShell {
packages = [
oldpkgs.zsh
];
}

View File

@ -0,0 +1,20 @@
{
lib,
stdenv,
fetchzip,
version,
hash,
}:
stdenv.mkDerivation {
pname = "hello";
version = "${version}";
src = fetchzip {
url = "https://ftp.gnu.org/gnu/hello/hello-${version}.tar.gz";
hash = "${hash}";
};
installPhase = ''
./hello --version
mkdir -p $out/bin
cp hello $out/bin/hello
'';
}

View File

@ -0,0 +1,10 @@
{ pkgs ? import <nixpkgs> {} }:
let
commonBuild = import ./build.nix;
callPackageForVersion = { version, hash }: pkgs.callPackage commonBuild { version = version; hash = hash; };
in
{
p0 = callPackageForVersion { version = "2.11"; hash = "sha256-1nXiB0vMCmMYmikTopb/j5WKFwHkc7xoLOqL9/bDFak="; };
p1 = callPackageForVersion { version = "2.12"; hash = "sha256-4GQeKLIxoWfYiOraJub5RsHNVQBr2H+3bfPP22PegdU="; };
p2 = callPackageForVersion { version = "2.12.1"; hash = "sha256-1kJjhtlsAkpNB7f6tZEs+dbKd8z7KoNHyDHEJ0tmhnc="; };
}

View File

@ -0,0 +1,10 @@
# From https://nix.dev/tutorials/nixos/building-and-running-docker-images.html
{ pkgs ? import <nixpkgs> { }
, pkgsLinux ? import <nixpkgs> { system = "x86_64-linux"; }
}:
pkgs.dockerTools.buildImage {
name = "hello-docker";
config = {
Cmd = [ "${pkgsLinux.hello}/bin/hello" ];
};
}

View File

@ -0,0 +1,38 @@
with import <nixpkgs> {}; stdenv.mkDerivation {
name = "hello";
src = fetchFromGitHub {
owner = "emersion";
repo = "hello-wayland";
rev = "5f3a35def81116f0a74fcaf5a421d66c6700482d";
hash = "sha256-gcLR8gosQlPPgFrxqmRQ6/59RjAfJNX6CcsYP+L+A58=";
};
# Dependencies
# buildInputs are for programs and libraries used by the new derivation at run-time
buildInputs = [
wayland
wayland-protocols
];
# nativeBuildInputs are for programs and libraries used at build-time that, if they are a compiler or similar tool,
# produce code to run at run-time—i.e. tools used to build the new derivation
nativeBuildInputs = with pkgs; [
coreutils
gcc
imagemagick
pkg-config
];
configurePhase = ''
echo "Nope"
'';
buildPhase = ''
make
'';
installPhase = ''
mkdir -p "$out/bin"
cp ./hello-wayland "$out/bin/"
'';
}

View File

@ -0,0 +1,14 @@
with import <nixpkgs> {}; buildGoModule {
src = fetchFromGitHub {
owner = "eliben";
repo = "static-server";
rev = "c4f0c0c3fa1ab24de43e3f25fc2eaa17d76be078";
sha256 = "sha256-Lae5fWcW+rGkyGMIlOH3DVCjE+JiAGVgLi+s/5p08cc=";
};
name = "static-server";
vendorHash = "sha256-1p3dCLLo+MTPxf/Y3zjxTagUi+tq7nZSj4ZB/aakJGY=";
checkPhase = ''
echo "bypass tests..."
'';
}

26
hello-world/default.nix Normal file
View File

@ -0,0 +1,26 @@
with import <nixpkgs> {}; stdenv.mkDerivation {
name = "hello";
# Source Code
# See: https://nixos.org/nixpkgs/manual/#ssec-unpack-phase
src = ./src;
# Dependencies
# See: https://nixos.org/nixpkgs/manual/#ssec-stdenv-dependencies
buildInputs = [ coreutils gcc ];
# Build Phases
# See: https://nixos.org/nixpkgs/manual/#sec-stdenv-phases
configurePhase = ''
declare -xp
'';
buildPhase = ''
gcc "$src/hello.c" -o ./hello
'';
installPhase = ''
mkdir -p "$out/bin"
cp ./hello "$out/bin/"
'';
}

8
hello-world/src/hello.c Normal file
View File

@ -0,0 +1,8 @@
#include <stdio.h>
int main(int argn, char **argv)
{
printf("hello world!\n");
return 0;
}

View File

@ -0,0 +1,3 @@
# shaka-packager
Source: https://fasterthanli.me/series/building-a-rust-service-with-nix/part-9#a-real-world-mkderivation

View File

@ -0,0 +1,30 @@
with
(import <nixpkgs> { });
let
version = "2.6.1";
in
stdenv.mkDerivation
{
name = "shaka-packager-${version}";
# https://nixos.wiki/wiki/Packaging/Binaries
src = pkgs.fetchurl {
url =
"https://github.com/shaka-project/shaka-packager/releases/download/v${version}/packager-linux-x64";
sha256 = "sha256-MoMX6PEtvPmloXJwRpnC2lHlT+tozsV4dmbCqweyyI0=";
};
dontUnpack = true;
sourceRoot = ".";
installPhase = ''
install -m755 -D $src $out/bin/shaka-packager
'';
meta = with lib; {
homepage = "https://shaka-project.github.io/shaka-packager/html/";
description =
"Media packaging framework for VOD and Live DASH and HLS applications";
platforms = platforms.x86_64-linux;
};
}

20
simple-shell/README.md Normal file
View File

@ -0,0 +1,20 @@
# shell
## Usage
```sh
> nix-shell shell.nix --command "cowsay hey"
_____
< hey >
-----
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
> nix-shell shell2.nix --command "nvim --version"
NVIM v0.9.5
Build type: Release
LuaJIT 2.1.1693350652
```

7
simple-shell/shell.nix Normal file
View File

@ -0,0 +1,7 @@
with
import <nixpkgs> {};
mkShell {
packages = [
cowsay
];
}

9
simple-shell/shell2.nix Normal file
View File

@ -0,0 +1,9 @@
{ pkgs ? import <nixpkgs> { } }:
with pkgs;
mkShell
{
packages = [
neovim
];
}