nixpkgs is a flake

This commit is contained in:
Daniel Flanagan 2024-07-08 13:26:43 -05:00
parent 2fc64a9e97
commit 3f301a41ee

View file

@ -1,6 +1,6 @@
This post is a Flake-based rewrite of [Learn Nix the Fun Way on fzakaria.com][0]. This post is a Flake-based rewrite of [Learn Nix the Fun Way on fzakaria.com][0].
I really enjoyed the content of the post and wanted to write it as a Nix I really enjoyed the content of the post and wanted to write it as a Nix
user who is just using and prefers flakes. It does add a few extra steps and user who is just using and prefers Flakes. It does add a few extra steps and
complexity, but I think it's still valuable and perhaps reveals a bit more about complexity, but I think it's still valuable and perhaps reveals a bit more about
Nix and why it's pretty fantastic. Nix and why it's pretty fantastic.
@ -20,7 +20,7 @@ Sure, it's _sort of portable_, if you tell the person running it to have _curl_
Nix **guarantees** portability. Nix **guarantees** portability.
We might leverage _[Nixpkgs' trivial builders](https://ryantm.github.io/nixpkgs/builders/trivial-builders/)_ (specifically, `writeShellScriptBin`) in a basic Nix flake to turn this into a Nix derivation (i.e. build recipe): We might leverage _[Nixpkgs' trivial builders](https://ryantm.github.io/nixpkgs/builders/trivial-builders/)_ (specifically, `writeShellScriptBin`) in a basic Nix Flake to turn this into a Nix derivation (i.e. build recipe):
```nix ```nix
# flake.nix # flake.nix
@ -225,19 +225,19 @@ $ what-is-my-ip
We took a relatively simple script through a variety of applications in the Nix ecosystem: build recipe, shell, docker image, and finally NixOS VM. We took a relatively simple script through a variety of applications in the Nix ecosystem: build recipe, shell, docker image, and finally NixOS VM.
One of the super neat part about flakes is that anywhere you find a flake, you One of the super neat part about Flakes is that anywhere you find a Flake, you
can make use of it. Try it out now! can make use of it. Try it out now!
> **NOTE**: The following obviously runs code from the internet. Be wary of doing this in general. > **NOTE**: The following obviously runs code from the internet. Be wary of doing this in general.
Run a flake's package: Run a Flake's package:
```console ```console
$ nix run git+https://git.lyte.dev/lytedev/learn-flakes-the-fun-way $ nix run git+https://git.lyte.dev/lytedev/learn-flakes-the-fun-way
24.5.113.148 24.5.113.148
``` ```
Enter a flake's development environment: Enter a Flake's development environment:
```console ```console
$ nix develop git+https://git.lyte.dev/lytedev/learn-flakes-the-fun-way -c $SHELL $ nix develop git+https://git.lyte.dev/lytedev/learn-flakes-the-fun-way -c $SHELL
@ -250,7 +250,7 @@ $ nix develop github:helix-editor/helix
# yeah, seriously! that's it! # yeah, seriously! that's it!
``` ```
Load a flake's docker image and run it: Load a Flake's docker image and run it:
```console ```console
$ docker load < $(nix build git+https://git.lyte.dev/lytedev/learn-flakes-the-fun-way#container --print-out-paths) $ docker load < $(nix build git+https://git.lyte.dev/lytedev/learn-flakes-the-fun-way#container --print-out-paths)
@ -258,7 +258,7 @@ Loaded image: what-is-my-ip-container:wg0z43v4sc1qhq7rsqg02w80vsfk9dl0
$ docker run -it what-is-my-ip-container:c9g6x30invdq1bjfah3w1aw5w52vkdfn $ docker run -it what-is-my-ip-container:c9g6x30invdq1bjfah3w1aw5w52vkdfn
``` ```
Run a flake's NixOS configuration as a virtual machine: Run a Flake's NixOS configuration as a virtual machine:
```console ```console
$ nixos-rebuild build-vm --flake git+https://git.lyte.dev/lytedev/learn-flakes-the-fun-way#default $ nixos-rebuild build-vm --flake git+https://git.lyte.dev/lytedev/learn-flakes-the-fun-way#default
@ -277,9 +277,9 @@ Done. The virtual machine can be started by running /nix/store/abc-nixos-vm/bin
$ /nix/store/abc-nixos-vm/bin/run-dragon-vm $ /nix/store/abc-nixos-vm/bin/run-dragon-vm
``` ```
Finally, Flakes can take any other Flake as input and use them for their own outputs. This means that they compose wonderfully. Any Flake can use anything from any other Flake. They're basically big ol' distributed functions (if you squint just right). Finally, Flakes can take any other Flake as input and use them for their own outputs. This means that they compose wonderfully. Any Flake can use anything from any other Flake. They're basically big ol' distributed functions (if you squint just right). `nixpkgs` itself is "simply" a huge Flake.
You can generate your own flake however you like; Flakes provide templating You can generate your own Flake however you like; Flakes provide templating
facilities. Here, you can use my very own template: facilities. Here, you can use my very own template:
```console ```console
@ -349,6 +349,6 @@ Hopefully, seeing the _fun things_ you can do with Nix might inspire you to push
There is a golden pot 💰 at the end of this rainbow 🌈 awaiting you. There is a golden pot 💰 at the end of this rainbow 🌈 awaiting you.
**Learn Nix the fun way.** **Learn Flakes the fun way.**
[0]: https://fzakaria.com/2024/07/05/learn-nix-the-fun-way.html [0]: https://fzakaria.com/2024/07/05/learn-nix-the-fun-way.html