Add section on using other flakes in your own

This commit is contained in:
Daniel Flanagan 2024-07-08 13:20:07 -05:00
parent 36b87c1813
commit 1319512c95

View file

@ -230,28 +230,46 @@ 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:
```console ```console
# run a flake's package
$ 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
$ 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
Hello, Nix! Hello, Nix!
$ what-is-my-ip $ what-is-my-ip
24.5.113.148 24.5.113.148
# load a flake's docker image and run it # want to hack on the Helix text editor?
$ nix develop github:helix-editor/helix
# yeah, seriously! that's it!
```
Load a flake's docker image and run it:
```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)
Loaded image: what-is-my-ip-container:wg0z43v4sc1qhq7rsqg02w80vsfk9dl0 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
$ 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
Done. The virtual machine can be started by running /nix/store/xswwdly9m5bwhcz9ajd6km5hx9vdmfzw-nixos-vm/bin/run-nixos-vm Done. The virtual machine can be started by running /nix/store/xswwdly9m5bwhcz9ajd6km5hx9vdmfzw-nixos-vm/bin/run-nixos-vm
$ /nix/store/xswwdly9m5bwhcz9ajd6km5hx9vdmfzw-nixos-vm/bin/run-nixos-vm $ /nix/store/xswwdly9m5bwhcz9ajd6km5hx9vdmfzw-nixos-vm/bin/run-nixos-vm
```
# like running a pre-configured version of my workstation's NixOS configuration Run an exact replica my workstation in a virtual machine (as configured via Nix
-- meaning it will not have my passwords on it 😉):
```console
# NOTE: this will probably take a good, long time to build and lots of bandwidth # NOTE: this will probably take a good, long time to build and lots of bandwidth
# NOTE: you probably won't even be able to login # NOTE: you probably won't even be able to login
$ nixos-rebuild build-vm --flake git+https://git.lyte.dev/lytedev/nix#dragon $ nixos-rebuild build-vm --flake git+https://git.lyte.dev/lytedev/nix#dragon
@ -259,6 +277,68 @@ 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).
You can generate your own flake however you like; Flakes provide templating
facilities. Here, you can use my very own template:
```console
$ mkdir -p ~/my-fun-flake
$ cd ~/my-fun-flake
$ nix flake init --template git+https://git.lyte.dev/lytedev/nix#nix-flake
$ git init
$ git add -A
$ git commit -am 'initial commit'
$ nix develop -c $SHELL
```
And now you have everything you need to work on a Nix flake as if you were me!
To add the package from this tutorial, we can simply do this:
```diff
diff --git a/flake.nix b/flake.nix
index 408d4f2..c5d6883 100644
--- a/flake.nix
+++ b/flake.nix
@@ -2,16 +2,20 @@
inputs.pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix";
inputs.pre-commit-hooks.inputs.nixpkgs.follows = "nixpkgs";
+ inputs.learn-flakes-the-fun-way.url = "git+https://git.lyte.dev/lytedev/learn-flakes-the-fun-way";
+ inputs.learn-flakes-the-fun-way.inputs.nixpkgs.follows = "nixpkgs";
+
outputs = {
self,
nixpkgs,
pre-commit-hooks,
+ learn-flakes-the-fun-way,
...
}: let
systems = ["aarch64-linux" "aarch64-darwin" "x86_64-darwin" "x86_64-linux"];
forSystems = nixpkgs.lib.genAttrs systems;
in {
formatter = genPkgs (pkgs: pkgs.alejandra);
@@ -26,7 +30,7 @@
devShells = genPkgs (pkgs: {
nix = pkgs.mkShell {
- packages = with pkgs; [nil alejandra];
+ packages = with pkgs; [nil alejandra learn-flakes-the-fun-way.packages.${pkgs.system}.default];
inherit (self.outputs.checks.${pkgs.system}.pre-commit-check) shellHook;
};
```
Afterwards you can re-enter your development shell and run our script:
```console
$ nix develop -c $SHELL
$ what-is-my-ip
24.5.113.148
```
Hopefully, seeing the _fun things_ you can do with Nix might inspire you to push through the hard parts. Hopefully, seeing the _fun things_ you can do with Nix might inspire you to push through the hard parts.
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.