From 18597260bf4e1e7a4993db913e4d667502038317 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Mon, 8 Jul 2024 12:18:24 -0500 Subject: [PATCH] Nix copy done --- flake.nix | 17 ++++++++++-- post.md | 78 ++++++++++++++++++++++++++++++++++--------------------- 2 files changed, 63 insertions(+), 32 deletions(-) diff --git a/flake.nix b/flake.nix index 2a99357..99d6d52 100644 --- a/flake.nix +++ b/flake.nix @@ -1,11 +1,24 @@ { inputs.nixpkgs.url = "nixpkgs/nixos-24.05"; - outputs = {nixpkgs, ...}: let + outputs = { + self, + nixpkgs, + ... + }: let systems = ["aarch64-linux" "aarch64-darwin" "x86_64-darwin" "x86_64-linux"]; pkgsFor = func: (nixpkgs.lib.genAttrs systems (system: (func (import nixpkgs {inherit system;})))); in { packages = pkgsFor (pkgs: { - what-is-my-ip = pkgs.callPackage ./what-is-my-ip.nix {}; + default = pkgs.callPackage ./what-is-my-ip.nix {}; + }); + + devShells = pkgsFor (pkgs: { + default = pkgs.mkShell { + packages = [self.outputs.packages.${pkgs.system}.default]; + shellHook = '' + echo "Hello, Nix!" + ''; + }; }); }; } diff --git a/post.md b/post.md index 1d61b49..5316082 100644 --- a/post.md +++ b/post.md @@ -11,9 +11,9 @@ it's pretty fantastic. Let's walk through a single example of a shell script one may write: _what-is-my-ip_ ```bash -#! /usr/bin/env bash -curl -s http://httpbin.org/get | \ - jq --raw-output .origin +#!/usr/bin/env bash +curl -s http://httpbin.org/get \ + | jq --raw-output .origin ``` Sure, it's _sort of portable_, if you tell the person running it to have _curl_ and _jq_. What if you relied on a specific version of either though? @@ -31,7 +31,7 @@ We might leverage _[Nixpkgs' trivial builders](https://ryantm.github.io/nixpkgs/ pkgsFor = func: (nixpkgs.lib.genAttrs systems (system: (func (import nixpkgs {inherit system;})))); in { packages = pkgsFor (pkgs: { - what-is-my-ip = pkgs.callPackage ./what-is-my-ip.nix {}; + default = pkgs.callPackage ./what-is-my-ip.nix {}; }); }; } @@ -50,21 +50,21 @@ pkgs.writeShellScriptBin "what-is-my-ip" '' Here we are pinning our package to dependencies which come from NixOS/Nixpkgs release branch 24.05. -If we `nix build .#what-is-my-ip` and `readlink result` we get: +If we `nix build` and `readlink result` we get: **/nix/store/lr6wlz2652r35rwzc79samg77l6iqmii-what-is-my-ip** -And, of course, we can run our result: +And, of course, we can run our built result: -```bash +```console $ ./result/bin/what-is-my-ip 24.5.113.148 ``` -Or run it from the Flake: +Or run it from the Flake directly: -```bash -$ nix run .#what-is-my-ip +```console +$ nix run 24.5.113.148 ``` @@ -77,26 +77,46 @@ $ nix-store --query --graph $(readlink result) | nix shell nixpkgs#graphviz -c d [![Image of what-is-my-ip dependencies as a graph](/assets/images/what-is-my-ip-deps.png)](/assets/images/what-is-my-ip-deps.png) Let's create a _developer environment_ and bring in our new tool. -This is a great way to create developer environment with reproducible tools +This is a great way to create developer environments with reproducible tools. -```nix -let - pkgs = import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/nixos-24.05.tar.gz") {}; - what-is-my-ip = import ./what-is-my-ip.nix {inherit pkgs;}; -in - pkgs.mkShell { - packages = [what-is-my-ip]; - shellHook = '' - echo "Hello, Nix!" - ''; - } +```diff +diff --git a/flake.nix b/flake.nix +index 2a99357..ab32421 100644 +--- a/flake.nix ++++ b/flake.nix +@@ -1,11 +1,24 @@ + { + inputs.nixpkgs.url = "nixpkgs/nixos-24.05"; +- outputs = {nixpkgs, ...}: let ++ outputs = { ++ self, ++ nixpkgs, ++ ... ++ }: let + systems = ["aarch64-linux" "aarch64-darwin" "x86_64-darwin" "x86_64-linux"]; + pkgsFor = func: (nixpkgs.lib.genAttrs systems (system: (func (import nixpkgs {inherit system;})))); + in { + packages = pkgsFor (pkgs: { + what-is-my-ip = pkgs.callPackage ./what-is-my-ip.nix {}; + }); ++ ++ devShells = pkgsFor (pkgs: { ++ default = pkgs.mkShell { ++ packages = [self.outputs.packages.${pkgs.system}.what-is-my-ip]; ++ shellHook = '' ++ echo "Hello, Nix!" ++ ''; ++ }; ++ }); + }; + } ``` -``` -❯ nix-shell what-is-my-ip-shell.nix +```console +$ nix develop -c $SHELL Hello, Nix! -[nix-shell:~/tutorial]$ which what-is-my-ip +$ which what-is-my-ip /nix/store/lr6wlz2652r35rwzc79samg77l6iqmii-what-is-my-ip/bin/what-is-my-ip ``` @@ -104,11 +124,9 @@ Hello, Nix! We can now do binary or source deployments 🚀🛠️📦 since we know the full dependency closure of our tool. We simply copy the necessary _/nix/store_ paths to another machine with Nix installed. -```bash -❯ nix copy --to ssh://nixie.tail9f4b5.ts.net \ - $(nix-build what-is-my-ip.nix) --no-check-sigs - -❯ ssh nixie.tail9f4b5.ts.net +```console +$ nix copy --to ssh://beefcake $(nix build --print-out-paths) +$ ssh beefcake [fmzakari@nixie:~]$ /nix/store/lr6wlz2652r35rwzc79samg77l6iqmii-what-is-my-ip/bin/what-is-my-ip 98.147.178.19