From c56d09816f7293f2f9d0e425924a0e9352919e1d Mon Sep 17 00:00:00 2001 From: Patrick MARIE Date: Sun, 17 Mar 2024 17:33:12 +0100 Subject: [PATCH] Initial commit --- .gitignore | 2 + README.md | 66 ++++++++++++++++++ TODO.md | 5 ++ flake-basic/README.md | 101 ++++++++++++++++++++++++++++ flake-basic/flake.lock | 27 ++++++++ flake-basic/flake.nix | 19 ++++++ flake-multi-platform/README.md | 19 ++++++ flake-multi-platform/flake.lock | 61 +++++++++++++++++ flake-multi-platform/flake.nix | 25 +++++++ hello-derivation/default.nix | 16 +++++ hello-derivation/hello_builder.sh | 12 ++++ hello-versions/shell.nix | 16 +++++ hello-world-callpackage/build.nix | 20 ++++++ hello-world-callpackage/default.nix | 10 +++ hello-world-docker/default.nix | 10 +++ hello-world-github/default.nix | 38 +++++++++++ hello-world-go/default.nix | 14 ++++ hello-world/default.nix | 26 +++++++ hello-world/src/hello.c | 8 +++ samples/shaka-packager/README.md | 3 + samples/shaka-packager/default.nix | 30 +++++++++ simple-shell/README.md | 20 ++++++ simple-shell/shell.nix | 7 ++ simple-shell/shell2.nix | 9 +++ 24 files changed, 564 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 TODO.md create mode 100644 flake-basic/README.md create mode 100644 flake-basic/flake.lock create mode 100644 flake-basic/flake.nix create mode 100644 flake-multi-platform/README.md create mode 100644 flake-multi-platform/flake.lock create mode 100644 flake-multi-platform/flake.nix create mode 100644 hello-derivation/default.nix create mode 100644 hello-derivation/hello_builder.sh create mode 100644 hello-versions/shell.nix create mode 100644 hello-world-callpackage/build.nix create mode 100644 hello-world-callpackage/default.nix create mode 100644 hello-world-docker/default.nix create mode 100644 hello-world-github/default.nix create mode 100644 hello-world-go/default.nix create mode 100644 hello-world/default.nix create mode 100644 hello-world/src/hello.c create mode 100644 samples/shaka-packager/README.md create mode 100644 samples/shaka-packager/default.nix create mode 100644 simple-shell/README.md create mode 100644 simple-shell/shell.nix create mode 100644 simple-shell/shell2.nix diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c8c616 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +result + diff --git a/README.md b/README.md new file mode 100644 index 0000000..9fb1abf --- /dev/null +++ b/README.md @@ -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 + \ No newline at end of file diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..cd7c4b8 --- /dev/null +++ b/TODO.md @@ -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); \ No newline at end of file diff --git a/flake-basic/README.md b/flake-basic/README.md new file mode 100644 index 0000000..44c1aaa --- /dev/null +++ b/flake-basic/README.md @@ -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" ]; + }; + }; +} +``` + + + diff --git a/flake-basic/flake.lock b/flake-basic/flake.lock new file mode 100644 index 0000000..1369e9b --- /dev/null +++ b/flake-basic/flake.lock @@ -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 +} diff --git a/flake-basic/flake.nix b/flake-basic/flake.nix new file mode 100644 index 0000000..c61d895 --- /dev/null +++ b/flake-basic/flake.nix @@ -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" ]; + }; + }; +} diff --git a/flake-multi-platform/README.md b/flake-multi-platform/README.md new file mode 100644 index 0000000..08876ac --- /dev/null +++ b/flake-multi-platform/README.md @@ -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' +``` diff --git a/flake-multi-platform/flake.lock b/flake-multi-platform/flake.lock new file mode 100644 index 0000000..994fb9c --- /dev/null +++ b/flake-multi-platform/flake.lock @@ -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 +} diff --git a/flake-multi-platform/flake.nix b/flake-multi-platform/flake.nix new file mode 100644 index 0000000..400958c --- /dev/null +++ b/flake-multi-platform/flake.nix @@ -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" ]; + }; + } + ); +} \ No newline at end of file diff --git a/hello-derivation/default.nix b/hello-derivation/default.nix new file mode 100644 index 0000000..0dd5cab --- /dev/null +++ b/hello-derivation/default.nix @@ -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; + } \ No newline at end of file diff --git a/hello-derivation/hello_builder.sh b/hello-derivation/hello_builder.sh new file mode 100644 index 0000000..185f9ee --- /dev/null +++ b/hello-derivation/hello_builder.sh @@ -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 . +cd $src +ls -l +./configure --prefix=$out +make +make install \ No newline at end of file diff --git a/hello-versions/shell.nix b/hello-versions/shell.nix new file mode 100644 index 0000000..14d26c3 --- /dev/null +++ b/hello-versions/shell.nix @@ -0,0 +1,16 @@ +# https://lazamar.co.uk/nix-versions/ + +# The following will: +# - load 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 {}) +}: +pkgs.mkShell { + packages = [ + oldpkgs.zsh + ]; +} \ No newline at end of file diff --git a/hello-world-callpackage/build.nix b/hello-world-callpackage/build.nix new file mode 100644 index 0000000..e99dc52 --- /dev/null +++ b/hello-world-callpackage/build.nix @@ -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 + ''; +} \ No newline at end of file diff --git a/hello-world-callpackage/default.nix b/hello-world-callpackage/default.nix new file mode 100644 index 0000000..cb31286 --- /dev/null +++ b/hello-world-callpackage/default.nix @@ -0,0 +1,10 @@ +{ pkgs ? import {} }: +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="; }; +} diff --git a/hello-world-docker/default.nix b/hello-world-docker/default.nix new file mode 100644 index 0000000..4ce651d --- /dev/null +++ b/hello-world-docker/default.nix @@ -0,0 +1,10 @@ +# From https://nix.dev/tutorials/nixos/building-and-running-docker-images.html +{ pkgs ? import { } +, pkgsLinux ? import { system = "x86_64-linux"; } +}: +pkgs.dockerTools.buildImage { + name = "hello-docker"; + config = { + Cmd = [ "${pkgsLinux.hello}/bin/hello" ]; + }; +} \ No newline at end of file diff --git a/hello-world-github/default.nix b/hello-world-github/default.nix new file mode 100644 index 0000000..31c6868 --- /dev/null +++ b/hello-world-github/default.nix @@ -0,0 +1,38 @@ +with import {}; 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/" + ''; +} \ No newline at end of file diff --git a/hello-world-go/default.nix b/hello-world-go/default.nix new file mode 100644 index 0000000..da40048 --- /dev/null +++ b/hello-world-go/default.nix @@ -0,0 +1,14 @@ +with import {}; 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..." + ''; +} \ No newline at end of file diff --git a/hello-world/default.nix b/hello-world/default.nix new file mode 100644 index 0000000..3229b61 --- /dev/null +++ b/hello-world/default.nix @@ -0,0 +1,26 @@ +with import {}; 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/" + ''; +} \ No newline at end of file diff --git a/hello-world/src/hello.c b/hello-world/src/hello.c new file mode 100644 index 0000000..8df34d8 --- /dev/null +++ b/hello-world/src/hello.c @@ -0,0 +1,8 @@ +#include + +int main(int argn, char **argv) +{ + printf("hello world!\n"); + + return 0; +} \ No newline at end of file diff --git a/samples/shaka-packager/README.md b/samples/shaka-packager/README.md new file mode 100644 index 0000000..a3ab444 --- /dev/null +++ b/samples/shaka-packager/README.md @@ -0,0 +1,3 @@ +# shaka-packager + +Source: https://fasterthanli.me/series/building-a-rust-service-with-nix/part-9#a-real-world-mkderivation diff --git a/samples/shaka-packager/default.nix b/samples/shaka-packager/default.nix new file mode 100644 index 0000000..fd320da --- /dev/null +++ b/samples/shaka-packager/default.nix @@ -0,0 +1,30 @@ +with +(import { }); +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; + }; +} diff --git a/simple-shell/README.md b/simple-shell/README.md new file mode 100644 index 0000000..443f665 --- /dev/null +++ b/simple-shell/README.md @@ -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 +``` diff --git a/simple-shell/shell.nix b/simple-shell/shell.nix new file mode 100644 index 0000000..90eec5e --- /dev/null +++ b/simple-shell/shell.nix @@ -0,0 +1,7 @@ +with + import {}; +mkShell { + packages = [ + cowsay + ]; +} diff --git a/simple-shell/shell2.nix b/simple-shell/shell2.nix new file mode 100644 index 0000000..9a4e7b6 --- /dev/null +++ b/simple-shell/shell2.nix @@ -0,0 +1,9 @@ +{ pkgs ? import { } }: +with pkgs; +mkShell +{ + packages = [ + neovim + ]; +} +