From 7de7ca85321035a1ac234ff830b52689b7a7d83a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amy=20de=20Buitl=C3=A9ir?= Date: Wed, 3 Sep 2025 20:35:34 +0100 Subject: [PATCH] temp --- index.html | 408 ++++++++++++++++++--- shell.nix | 1 + source/recipes/devshell/tempwork/flake.nix | 20 + source/recipes/main.adoc | 8 + source/recipes/remote-git-flake/main.adoc | 2 + 5 files changed, 393 insertions(+), 46 deletions(-) create mode 100644 source/recipes/devshell/tempwork/flake.nix diff --git a/index.html b/index.html index 42df385..2cf247f 100644 --- a/index.html +++ b/index.html @@ -241,50 +241,83 @@ pre.pygments .tok-il { color: #666666 } /* Literal.Number.Integer.Long */
  • 10. Recipes
    -

    10.1. Access to a top-level package from the Nixpkgs/NixOS repo

    +

    10.1. Running programs directly (without installing them)

    +
    +

    10.1.1. A top level package from the Nixpkgs/NixOS repo

    +
    +
    +
    $ nix run nixpkgs#cowsay "Moo!"
    + ______
    +< Moo! >
    + ------
    +        \   ^__^
    +         \  (oo)\_______
    +            (__)\       )\/\
    +                ||----w |
    +                ||     ||
    +
    +
    +
    +
    +

    10.1.2. Flakes

    +
    +
    A flake defined in a local file
    +
    +
    +
    $ nix run ~/codeberg/hello-flake
    +Hello from your flake!
    +
    +
    +
    +
    +
    A flake defined in a remote git repo
    +
    +
    +
    $ nix run git+https://codeberg.org/mhwombat/hello-flake
    +Hello from your flake!
    +
    +
    +
    +

    To run a specific branch, use the command below.

    +
    +
    +
    +
    nix run git+https://codeberg.org/mhwombat/hello-flake?ref=main
    +
    +
    +
    +

    To run a specific branch and revision, use the command below.

    +
    +
    +
    +
    nix run git+https://codeberg.org/mhwombat/hello-flake?ref=main&rev=d44728bce88a6f9d1d37dbf4720ece455e997606
    +
    +
    +
    +
    +
    A flake defined in a zip archive
    +
    +
    +
    $ nix run https://codeberg.org/mhwombat/hello-flake/archive/main.zip
    +Hello from your flake!
    +
    +
    +
    +
    +
    A flake defined in a compressed tar archive
    +
    +
    +
    $ nix run https://codeberg.org/mhwombat/hello-flake/archive/main.tar.gz
    +Hello from your flake!
    +
    +
    +
    + +
    +
    +
    +

    10.2. Ad hoc environments with access to…​

    +
    +

    10.2.1. A top level package from the Nixpkgs/NixOS repo

    +
    +
    +
    $ nix shell nixpkgs#cowsay
    +$ cowsay "moo"
    + _____
    +< moo >
    + -----
    +        \   ^__^
    +         \  (oo)\_______
    +            (__)\       )\/\
    +                ||----w |
    +                ||     ||
    +
    +
    +
    +
    +

    10.2.2. A flake

    +
    +
    +
    $ nix shell git+https://codeberg.org/mhwombat/hello-flake
    +$ hello-flake
    +Hello from your flake!
    +
    +
    +
    +

    You can use all of the flake reference styles defined in Section 10.1.2, “Flakes”.

    +
    +
    +
    +
    +

    10.3. Scripts with access to…​

    +
    +

    10.3.1. A top level package from the Nixpkgs/NixOS repo

    +
    +
    Script
    +
    +
    1
    +2
    +3
    +4
    #! /usr/bin/env nix
    +#! nix shell nixpkgs#hello nixpkgs#cowsay --command bash
    +hello
    +cowsay "Pretty cool, huh?"
    +
    +
    +
    +
    +
    Output
    +
    +
    Hello, world!
    + ___________________
    +< Pretty cool, huh? >
    + -------------------
    +        \   ^__^
    +         \  (oo)\_______
    +            (__)\       )\/\
    +                ||----w |
    +                ||     ||
    +
    +
    +
    +
    +

    10.3.2. A flake

    +
    +
    Script
    +
    +
    1
    +2
    +3
    #! /usr/bin/env nix
    +#! nix shell git+https://codeberg.org/mhwombat/hello-flake --command bash
    +hello-flake
    +
    +
    +
    +
    +
    Output
    +
    +
    Hello from your flake!
    +
    +
    +
    +

    You can use all of the flake reference styles defined in Section 10.1.2, “Flakes”.

    +
    +
    +
    +
    +

    10.4. Development shells with access to…​

    +
    +

    10.4.1. A top level package from the Nixpkgs/NixOS repo

    +
    +
    flake.nix
    +
    +
     1
    + 2
    + 3
    + 4
    + 5
    + 6
    + 7
    + 8
    + 9
    +10
    +11
    +12
    +13
    +14
    +15
    +16
    +17
    +18
    +19
    +20
    {
    +  inputs = {
    +    nixpkgs.url = "github:NixOS/nixpkgs";
    +    flake-utils.url = "github:numtide/flake-utils";
    +  };
    +
    +  outputs = { self, nixpkgs, flake-utils }:
    +    flake-utils.lib.eachDefaultSystem (system:
    +      let
    +        pkgs = import nixpkgs { inherit system; };
    +      in
    +      {
    +        devShells = rec {
    +          default = pkgs.mkShell {
    +            packages = [ pkgs.cowsay ];
    +          };
    +        };
    +      }
    +    );
    +}
    +
    +
    +
    +
    +

    Here’s a demonstration using the shell.

    +
    +
    +
    +
    $ cowsay "Moo!"
    +bash: line 35: cowsay: command not found
    +$ pwd
    +/home/amy/codeberg/nix-book/source/recipes/devshell/tempwork
    +$ realpath ../../../..
    +/home/amy/codeberg/nix-book
    +$ nix develop
    +warning: Git tree '/home/amy/codeberg/nix-book' is dirty
    +warning: creating lock file '"/home/amy/codeberg/nix-book/source/recipes/devshell/tempwork/flake.lock"':
    +• Added input 'flake-utils':
    +    'github:numtide/flake-utils/11707dc2f618dd54ca8739b309ec4fc024de578b?narHash=sha256-l0KFg5HjrsfsO/JpG%2Br7fRrqm12kzFHyUHqHCVpMMbI%3D' (2024-11-13)
    +• Added input 'flake-utils/systems':
    +    'github:nix-systems/default/da67096a3b9bf56a91d16901293e51ba5b49a27e?narHash=sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768%3D' (2023-04-09)
    +• Added input 'nixpkgs':
    +    'github:NixOS/nixpkgs/6ce002c32a5e3470505a44fbcfb9e51cbaec71a6?narHash=sha256-uOBDwpkJZMb8gjkOA89fx%2Bcq9qT0abLETCINC/cb%2Bz0%3D' (2025-09-03)
    +warning: Git tree '/home/amy/codeberg/nix-book' is dirty
    +$ cowsay "Moo!"
    + ______
    +< Moo! >
    + ------
    +        \   ^__^
    +         \  (oo)\_______
    +            (__)\       )\/\
    +                ||----w |
    +                ||     ||
    +
    +
    +
    +
    +

    10.4.2. A flake

    +
    +

    FINISH

    +
    +
    +

    FINISH

    +
    +
    +

    FINISH

    +
    +
    +

    FINISH

    +
    +
    +

    FINISH

    +
    +
    +

    You can use all of the flake reference styles defined in Section 10.1.2, “Flakes”.

    +
    +
    +
    +
    +

    10.5. Access to a top-level package from the Nixpkgs/NixOS repo

    Ex: Access two packages from nixpkgs: hello and cowsay.

    -

    10.1.1. From the command line (flake style)

    +

    10.5.1. From the command line (flake style)

    $ nix shell nixpkgs#hello nixpkgs#cowsay --command bash
    @@ -4122,7 +4428,7 @@ $ cowsay "moo"
     
    -

    10.1.2. From the command line (non-flake style)

    +

    10.5.2. From the command line (non-flake style)

    $ nix-shell -p "[hello cowsay]"
    @@ -4141,7 +4447,7 @@ $ cowsay "moo"
     
    -

    10.1.3. In flake.nix

    +

    10.5.3. In flake.nix

    See the example in Section 8.2, “Introducing a dependency” where defined a development shell in flake.nix @@ -4150,7 +4456,7 @@ and tested the shell.

    -

    10.1.4. In shell.nix

    +

    10.5.4. In shell.nix

    shell.nix
    @@ -4191,7 +4497,7 @@ $ cowsay "moo"
    -

    10.1.5. In a Bash script (flake style)

    +

    10.5.5. In a Bash script (flake style)

    Script
    @@ -4221,7 +4527,7 @@ cowsay "Pretty cool, huh?&q
    -

    10.1.6. In a Bash script (non-flake style)

    +

    10.5.6. In a Bash script (non-flake style)

    Script
    @@ -4252,7 +4558,7 @@ cowsay "Pretty cool, huh?&q
    -

    10.2. Access to a package defined in a remote git repo

    +

    10.6. Access to a package defined in a remote git repo

    Ex: Access a package (not a flake) called hello-nix, which is defined in a remote git repo on codeberg. @@ -4260,7 +4566,7 @@ To use a package from GitHub, GitLab, or any other public platform, modify the URL.

    -

    10.2.1. In shell.nix

    +

    10.6.1. In shell.nix

    shell.nix
    @@ -4299,7 +4605,7 @@ Hello from your nix package!
    -

    10.3. Access to a flake defined in a remote git repo

    +

    10.7. Access to a flake defined in a remote git repo

    Ex: Access a flake called hello-flake, which is defined in a remote git repo on codeberg. @@ -4307,7 +4613,17 @@ To use a package from GitHub, GitLab, or any other public platform, modify the URL.

    -

    10.3.1. In shell.nix

    +

    10.7.1. From the command line

    +
    +
    +
    $ nix shell git+https://codeberg.org/mhwombat/hello-flake
    +$ hello-flake
    +Hello from your flake!
    +
    +
    +
    +
    +

    10.7.2. In shell.nix

    shell.nix
    @@ -4348,7 +4664,7 @@ Hello from your flake!
    -

    10.4. Access to a Haskell library package in the nixpkgs repo (without a .cabal file)

    +

    10.8. Access to a Haskell library package in the nixpkgs repo (without a .cabal file)

    Occasionally you might want to run a short Haskell program that depends on a Haskell library, but you don’t want to bother writing a cabal file.

    @@ -4357,7 +4673,7 @@ but you don’t want to bother writing a cabal file.

    Example: Access the containers package from the haskellPackages set in the nixpkgs repo.

    -

    10.4.1. In shell.nix

    +

    10.8.1. In shell.nix

    shell.nix
    @@ -4423,7 +4739,7 @@ $ runghc Main.hs
    -

    10.4.2. In a Haskell script

    +

    10.8.2. In a Haskell script

    Script
    @@ -4468,14 +4784,14 @@ $ runghc Main.hs
    -

    10.5. Access to a Haskell package on your local computer

    +

    10.9. Access to a Haskell package on your local computer

    Ex: Access three Haskell packages (pandoc-linear-table, pandoc-logic-proof, and pandoc-columns) that are on my hard drive.

    -

    10.5.1. In shell.nix

    +

    10.9.1. In shell.nix

    shell.nix
    @@ -4512,7 +4828,7 @@ mkShell {
    -

    10.6. Access to a Haskell package on your local computer, with inter-dependencies

    +

    10.10. Access to a Haskell package on your local computer, with inter-dependencies

    Ex: Access four Haskell packages (pandoc-linear-table, pandoc-logic-proof, pandoc-columns and pandoc-maths-web) @@ -4520,7 +4836,7 @@ that are on my hard drive. The fourth package depends on the first three to build.

    -

    10.6.1. In shell.nix

    +

    10.10.1. In shell.nix

    shell.nix
    @@ -4567,7 +4883,7 @@ mkShell {
    -

    10.7. Access to a Python library package in the nixpkgs repo (without using a Python builder)

    +

    10.11. Access to a Python library package in the nixpkgs repo (without using a Python builder)

    Occasionally you might want to run a short Python program that depends on a Python library, but you don’t want to bother configuring a builder.

    @@ -4576,7 +4892,7 @@ but you don’t want to bother configuring a builder.

    Example: Access the html_sanitizer package from the python3nnPackages set in the nixpkgs repo.

    -

    10.7.1. In a Python script

    +

    10.11.1. In a Python script

    Script
    @@ -4614,12 +4930,12 @@ sanitized: <strong>some text</strong>
    -

    10.8. Set an environment variable

    +

    10.12. Set an environment variable

    Ex: Set the value of the environment variable FOO to “bar”

    -

    10.8.1. In shell.nix

    +

    10.12.1. In shell.nix

    shell.nix
    @@ -4653,7 +4969,7 @@ mkShell {
    diff --git a/shell.nix b/shell.nix index c14461a..388d385 100644 --- a/shell.nix +++ b/shell.nix @@ -11,4 +11,5 @@ mkShell { # rubyPackages.coderay # rubyPackages.rouge ]; +# outputs = [ "start-shell" ]; } diff --git a/source/recipes/devshell/tempwork/flake.nix b/source/recipes/devshell/tempwork/flake.nix new file mode 100644 index 0000000..83db46c --- /dev/null +++ b/source/recipes/devshell/tempwork/flake.nix @@ -0,0 +1,20 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, flake-utils }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { inherit system; }; + in + { + devShells = rec { + default = pkgs.mkShell { + packages = [ pkgs.cowsay ]; + }; + }; + } + ); +} diff --git a/source/recipes/main.adoc b/source/recipes/main.adoc index bca2b1d..8bedd45 100644 --- a/source/recipes/main.adoc +++ b/source/recipes/main.adoc @@ -24,6 +24,14 @@ should invoke `nix-shell`. The second should declares the script interpreter and any dependencies. +include::run/main-generated.adoc[leveloffset=+1] + +include::ad-hoc/main-generated.adoc[leveloffset=+1] + +include::shebang/main-generated.adoc[leveloffset=+1] + +include::devshell/main-generated.adoc[leveloffset=+1] + include::nixpkgs-pkg/main.adoc[leveloffset=+1] include::remote-git/main.adoc[leveloffset=+1] diff --git a/source/recipes/remote-git-flake/main.adoc b/source/recipes/remote-git-flake/main.adoc index c9acf8d..ad353fb 100644 --- a/source/recipes/remote-git-flake/main.adoc +++ b/source/recipes/remote-git-flake/main.adoc @@ -5,4 +5,6 @@ which is defined in a remote git repo on codeberg. To use a package from GitHub, GitLab, or any other public platform, modify the URL. +include::cli/main-generated.adoc[leveloffset=+1] + include::shell/main-generated.adoc[leveloffset=+1]