Nix copy done

This commit is contained in:
Daniel Flanagan 2024-07-08 12:18:24 -05:00
parent 06c5be472a
commit 18597260bf
2 changed files with 63 additions and 32 deletions

View file

@ -1,11 +1,24 @@
{ {
inputs.nixpkgs.url = "nixpkgs/nixos-24.05"; 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"]; systems = ["aarch64-linux" "aarch64-darwin" "x86_64-darwin" "x86_64-linux"];
pkgsFor = func: (nixpkgs.lib.genAttrs systems (system: (func (import nixpkgs {inherit system;})))); pkgsFor = func: (nixpkgs.lib.genAttrs systems (system: (func (import nixpkgs {inherit system;}))));
in { in {
packages = pkgsFor (pkgs: { 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!"
'';
};
}); });
}; };
} }

76
post.md
View file

@ -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_ Let's walk through a single example of a shell script one may write: _what-is-my-ip_
```bash ```bash
#! /usr/bin/env bash #!/usr/bin/env bash
curl -s http://httpbin.org/get | \ curl -s http://httpbin.org/get \
jq --raw-output .origin | 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? 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;})))); pkgsFor = func: (nixpkgs.lib.genAttrs systems (system: (func (import nixpkgs {inherit system;}))));
in { in {
packages = pkgsFor (pkgs: { 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. 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** **/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 $ ./result/bin/what-is-my-ip
24.5.113.148 24.5.113.148
``` ```
Or run it from the Flake: Or run it from the Flake directly:
```bash ```console
$ nix run .#what-is-my-ip $ nix run
24.5.113.148 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) [![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. 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 ```diff
let diff --git a/flake.nix b/flake.nix
pkgs = import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/nixos-24.05.tar.gz") {}; index 2a99357..ab32421 100644
what-is-my-ip = import ./what-is-my-ip.nix {inherit pkgs;}; --- a/flake.nix
in +++ b/flake.nix
pkgs.mkShell { @@ -1,11 +1,24 @@
packages = [what-is-my-ip]; {
shellHook = '' inputs.nixpkgs.url = "nixpkgs/nixos-24.05";
echo "Hello, Nix!" - 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!"
+ '';
+ };
+ });
};
} }
``` ```
``` ```console
nix-shell what-is-my-ip-shell.nix $ nix develop -c $SHELL
Hello, Nix! 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 /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. 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 ```console
nix copy --to ssh://nixie.tail9f4b5.ts.net \ $ nix copy --to ssh://beefcake $(nix build --print-out-paths)
$(nix-build what-is-my-ip.nix) --no-check-sigs $ ssh beefcake
ssh nixie.tail9f4b5.ts.net
[fmzakari@nixie:~]$ /nix/store/lr6wlz2652r35rwzc79samg77l6iqmii-what-is-my-ip/bin/what-is-my-ip [fmzakari@nixie:~]$ /nix/store/lr6wlz2652r35rwzc79samg77l6iqmii-what-is-my-ip/bin/what-is-my-ip
98.147.178.19 98.147.178.19