diff --git a/spotlight_courses/HS24_NixOS/examples/flake/flake.lock b/spotlight_courses/HS24_NixOS/examples/flake/flake.lock new file mode 100644 index 0000000000000000000000000000000000000000..16d59817f06286000a81cef0269308462ed2da49 --- /dev/null +++ b/spotlight_courses/HS24_NixOS/examples/flake/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1729880355, + "narHash": "sha256-RP+OQ6koQQLX5nw0NmcDrzvGL8HDLnyXt/jHhL1jwjM=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "18536bf04cd71abd345f9579158841376fdd0c5a", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/spotlight_courses/HS24_NixOS/examples/flake/flake.nix b/spotlight_courses/HS24_NixOS/examples/flake/flake.nix new file mode 100644 index 0000000000000000000000000000000000000000..4048aec030831683f7b8e23573cc8cd0e4498c21 --- /dev/null +++ b/spotlight_courses/HS24_NixOS/examples/flake/flake.nix @@ -0,0 +1,11 @@ +{ + inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; + outputs = { + self, + nixpkgs, + }: { + packages.x86_64-linux.what-is-my-ip = nixpkgs.legacyPackages.x86_64-linux.callPackage ./what-is-my-ip.nix {}; + + packages.x86_64-linux.default = self.packages.x86_64-linux.what-is-my-ip; + }; +} diff --git a/spotlight_courses/HS24_NixOS/examples/flake/what-is-my-ip.nix b/spotlight_courses/HS24_NixOS/examples/flake/what-is-my-ip.nix new file mode 100644 index 0000000000000000000000000000000000000000..65179a185114a2c09908d2450f75f9ee01794ca1 --- /dev/null +++ b/spotlight_courses/HS24_NixOS/examples/flake/what-is-my-ip.nix @@ -0,0 +1,7 @@ +{pkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/nixos-24.05.tar.gz") {}}: +pkgs.writeShellScriptBin "what-is-my-ip" '' + ${pkgs.curl}/bin/curl \ + -s http://httpbin.org/get | \ + ${pkgs.jq}/bin/jq \ + --raw-output .origin +'' diff --git a/spotlight_courses/HS24_NixOS/main.typ b/spotlight_courses/HS24_NixOS/main.typ index 9cfcac53d00182d474b6f8b49a6f17696df4fda3..a1ed1e0b1f930e2f574ba4776b37b05ab4d278ff 100644 --- a/spotlight_courses/HS24_NixOS/main.typ +++ b/spotlight_courses/HS24_NixOS/main.typ @@ -182,8 +182,10 @@ cat /nix/store/5jf23xp5nmqw8vhrjpgvbimrvd086v6z-what-is-my-ip/bin/what-is-my-ip /nix/store/7ic2z0yz5hxw7lzz6rxsrdfv72yfy9vv-jq-1.7.1-bin/bin/jq \ --raw-output .origin ``` -=== The dependencies of everything in this Package -// TODO: fix this +===== Which dependencies does this use? +- Well, curl and jq obviously, but anything else? + + #scale(80%)[ #raw-render( ```dot @@ -191,11 +193,6 @@ digraph G { graph[nodesep = 0.1]; node[height=0.01, width=0.01, margin = 0.04, fontsize = 10]; edge[minlen=1, arrowsize = 0.5]; -// width="900pt"; -// height="1800pt"; -// fixedsize="true"; -// rankdir = LR; -// ratio = "compress"; subgraph depglibc { "libpsl-0.21.5" [label = "libpsl-0.21.5", shape = box]; "openssl-3.0.14" [label = "openssl-3.0.14", shape = box]; @@ -403,8 +400,84 @@ $==>$ If your change ('override') a package, it will automatically be built loca -= Fakes += Flakes +== What are flakes? +- File-system-tree which contains a `flake.nix` at it's root +- Write nix expressions (packages!) in a sane way +- Proper dependency management + - Improve reproducibility +- Provide unform naming scheme for declaring packages and their dependencies +== Dead-simple flake +```nix +{ + inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; + outputs = { self, nixpkgs, }: { + packages.x86_64-linux.what-is-my-ip = nixpkgs.legacyPackages.x86_64-linux.callPackage ./what-is-my-ip.nix {}; + + packages.x86_64-linux.default = self.packages.x86_64-linux.what-is-my-ip; + }; +} +``` +== Let's build it! +``` +⯠nix build +⯠ls +flake.lock flake.nix result what-is-my-ip.nix +``` +- Hey, two new things! + - flake.lock + - result +== A further look at _result_ +``` +⯠ls -l +total 16 +-rw-r--r-- 1 cr users 567 Oct 30 18:34 flake.lock +-rw-r--r-- 1 cr users 312 Oct 30 18:34 flake.nix +lrwxrwxrwx 1 cr users 57 Oct 30 18:35 result -> /nix/store/hysmspp11fxnkgjsp032q9zinl1gdk5n-what-is-my-ip/ +-rw-r--r-- 1 cr users 247 Oct 30 18:34 what-is-my-ip.nix +``` +- Symlink to the location of the build package in the nix store +- Easy to test our package, just run `./result/bin/what-is-my-ip` + +== What's the _flake.lock_ doing there? +``` +⯠cat flake.lock +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1729880355, + "narHash": "sha256-RP+OQ6koQQLX5nw0NmcDrzvGL8HDLnyXt/jHhL1jwjM=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "18536bf04cd71abd345f9579158841376fdd0c5a", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} +``` +== So, what's great about flakes exactly? +- Keep track of all inputs, lock them +- version-control your dependencies + - something broken after a `nix flake update`? \ + switch back to the latest commit + - provide packages easily + - I can expose my sys = `nixos-rebuild` - uses `Nix` to manage almost *everything* about your system @@ -794,6 +867,7 @@ content = { ] ) -= Sources +== +== Sources - https://cohost.org/leftpaddotpy/post/798893-rechosting-a-banger