$ cat hello-flake
-#!/usr/bin/env sh
+#!/usr/bin/env sh
-cowsay "Hello from your flake!"
-
+cowsay "Hello from your flake!"
From bea4055fc97b15efbc932d320167c95f18ada3f9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Amy=20de=20Buitl=C3=A9ir?= hello-flake file, with the modified line highlighted.
$ cat hello-flake
-#!/usr/bin/env sh
+#!/usr/bin/env sh
-cowsay "Hello from your flake!"
-
+cowsay "Hello from your flake!"
{
# See https://github.com/mhwombat/nix-for-numbskulls/blob/main/flakes.md
@@ -1239,9 +1239,9 @@ The highlighted modifications below will accomplish that.
We restart the development shell and see that the cowsay command is
-now available and the script works. Because we’ve updated source files
-but haven’t `git commit`ed the new version, we get a warning message
+
Now we restart the development shell and see that the cowsay command is
+available and the script works. Because we’ve updated source files
+but haven’t git commited the new version, we get a warning message
about it being “dirty”. It’s just a warning, though; the script runs
correctly.
git push the changes until we’re ready to share t
$ git commit hello-flake flake.nix -m 'added bovine feature' -[main 481542e] added bovine feature +[main 8c8f6c7] added bovine feature 2 files changed, 7 insertions(+), 1 deletion(-) $ nix run ________________________ @@ -1420,16 +1420,23 @@ Initialized empty Git repository in /home/amy/codeberg/nix-book/source/python-fl-Next, we’ll create a simple Python program.
+-+hello.py-$ cat hello.py -#!/usr/bin/env python +
1 +2 +3 +4 +5 +6 +7 #!/usr/bin/env python -def main(): - print("Hello from inside a Python program built with a Nix flake!") +def main(): + print("Hello from inside a Python program built with a Nix flake!") -if __name__ == "__main__": - main()+if __name__ == "__main__": + main() +@@ -1447,11 +1454,20 @@ packages you need), and you want to experiment a bit first.The command to enter a temporary shell is
-+
nix-shell -ppackages
nix-shell -p packages-+If there are multiple packages, they should be separated by spaces. Note -that the command used here is
nix-shellwith a hyphen, notnix shell+If there are multiple packages, they should be separated by spaces.
+++
++ ++ + ++ +++The command used here is
nix-shellwith a hyphen, notnix shellwith a space; those are two different commands. In fact there are hyphenated and non-hyphenated versions of many Nix commands, and yes, it’s confusing. The non-hyphenated commands were introduced when support @@ -1460,6 +1476,10 @@ commands will be replaced with non-hyphenated versions. Until then, a useful rule of thumb is that non-hyphenated commands are for for working directly with flakes; hyphenated commands are for everything else.@@ -1479,21 +1499,33 @@ Packaging User Guide, especially the section on setup args.Let’s enter a shell with Python so we can test the program.
++setup.py-$ cat setup.py -#!/usr/bin/env python +
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +10 +11 +12 #!/usr/bin/env python -from setuptools import setup +from setuptools import setup -setup( - name='hello-flake-python', - version='0.1.0', - py_modules=['hello'], - entry_points={ - 'console_scripts': ['hello-flake-python = hello:main'] - }, -)+setup( + name='hello-flake-python', + version='0.1.0', + py_modules=['hello'], + entry_points={ + 'console_scripts': ['hello-flake-python = hello:main'] + }, +) +@@ -1592,57 +1624,105 @@ function.-If you put all the pieces together, your
flake.nixshould look something like this.+@@ -1683,7 +1760,7 @@ repo, and commit all important files.+flake.nix-$ cat flake.nix -{ - # See https://github.com/mhwombat/nix-for-numbskulls/blob/main/flakes.md - # for a brief overview of what each section in a flake should or can contain. +
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21 +22 +23 +24 +25 +26 +27 +28 +29 +30 +31 +32 +33 +34 +35 +36 +37 +38 +39 +40 +41 +42 +43 +44 +45 +46 +47 +48 { + # See https://github.com/mhwombat/nix-for-numbskulls/blob/main/flakes.md + # for a brief overview of what each section in a flake should or can contain. - description = "a very simple and friendly flake written in Python"; + description = "a very simple and friendly flake written in Python"; - inputs = { - nixpkgs.url = "github:NixOS/nixpkgs"; - flake-utils.url = "github:numtide/flake-utils"; - }; + 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; }; - python = pkgs.python3; - in - { - devShells = rec { - default = pkgs.mkShell { - packages = [ - # Python plus helper tools - (python.withPackages (ps: with ps; [ - virtualenv # Virtualenv - pip # The pip installer - ])) - ]; - }; - }; + outputs = { self, nixpkgs, flake-utils }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { inherit system; }; + python = pkgs.python3; + in + { + devShells = rec { + default = pkgs.mkShell { + packages = [ + # Python plus helper tools + (python.withPackages (ps: with ps; [ + virtualenv # Virtualenv + pip # The pip installer + ])) + ]; + }; + }; - packages = rec { - hello = python.pkgs.buildPythonApplication { - name = "hello-flake-python"; + packages = rec { + hello = python.pkgs.buildPythonApplication { + name = "hello-flake-python"; - buildInputs = with python.pkgs; [ pip ]; + buildInputs = with python.pkgs; [ pip ]; - src = ./.; - }; - default = hello; - }; + src = ./.; + }; + default = hello; + }; - apps = rec { - hello = flake-utils.lib.mkApp { drv = self.packages.${system}.hello; }; - default = hello; - }; - } - ); -}+ apps = rec { + hello = flake-utils.lib.mkApp { drv = self.packages.${system}.hello; }; + default = hello; + }; + } + ); +} +@@ -1667,9 +1747,6 @@ $ nix run warning: Git tree '/home/amy/codeberg/nix-book/source/python-flake/hello-python' is dirty warning: creating lock file '/home/amy/codeberg/nix-book/source/python-flake/hello-python/flake.lock' warning: Git tree '/home/amy/codeberg/nix-book/source/python-flake/hello-python' is dirty -this derivation will be built: - /nix/store/ad7ajqx55bfqgj9527ibmkqflbmf29bi-hello-flake-python.drv -building '/nix/store/ad7ajqx55bfqgj9527ibmkqflbmf29bi-hello-flake-python.drv'... Hello from inside a Python program built with a Nix flake!$ git add flake.lock $ git commit -a -m 'initial commit' -[master (root-commit) 1c4a0d6] initial commit +[master (root-commit) a98ed14] initial commit 4 files changed, 127 insertions(+) create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/source/modify-hello-flake/main.adoc0 b/source/modify-hello-flake/main.adoc0 index d1308a0..daa4e4b 100644 --- a/source/modify-hello-flake/main.adoc0 +++ b/source/modify-hello-flake/main.adoc0 @@ -91,8 +91,9 @@ $ sed -i 's/echo/cowsay/' hello-flake //// [source,nix,highlight=4] +.hello-flake .... -$ cat hello-flake +$# cat hello-flake .... Let’s test the modified script. @@ -117,13 +118,14 @@ $ cp ../flake.nix.new flake.nix //// [source,nix,highlight=18..22] +.flake.nix .... $# cat flake.nix .... -We restart the development shell and see that the `cowsay` command is -now available and the script works. Because we’ve updated source files -but haven’t `git commit`ed the new version, we get a warning message +Now we restart the development shell and see that the `cowsay` command is +available and the script works. Because we’ve updated source files +but haven’t ``git commit``ed the new version, we get a warning message about it being "`dirty`". It’s just a warning, though; the script runs correctly. diff --git a/source/python-flake/main.adoc0 b/source/python-flake/main.adoc0 index fb55292..1ec39e5 100644 --- a/source/python-flake/main.adoc0 +++ b/source/python-flake/main.adoc0 @@ -11,9 +11,14 @@ $ git init Next, we’ll create a simple Python program. +//// +$ curl https://codeberg.org/mhwombat/hello-flake-python/raw/branch/main/hello.py --silent --output hello.py +//// + +[source,python,linenums] +.hello.py .... -$# curl https://codeberg.org/mhwombat/hello-flake-python/raw/branch/main/hello.py --silent --output hello.py -$ cat hello.py +$# cat hello.py .... Before we package the program, let’s verify that it runs. We’re going to @@ -28,10 +33,13 @@ packages you need), and you want to experiment a bit first. The command to enter a temporary shell is -`nix-shell -p` _packages_ +`nix-shell -p __packages__` -If there are multiple packages, they should be separated by spaces. Note -that the command used here is `nix-shell` with a hyphen, not `nix shell` +If there are multiple packages, they should be separated by spaces. + +[NOTE] +==== +The command used here is `nix-shell` with a hyphen, not `nix shell` with a space; those are two different commands. In fact there are hyphenated and non-hyphenated versions of many Nix commands, and yes, it’s confusing. The non-hyphenated commands were introduced when support @@ -39,6 +47,7 @@ for flakes was added to Nix. I predict that eventually all hyphenated commands will be replaced with non-hyphenated versions. Until then, a useful rule of thumb is that non-hyphenated commands are for for working directly with flakes; hyphenated commands are for everything else. +==== Let’s enter a shell with Python so we can test the program. @@ -56,9 +65,14 @@ Packaging User Guide], especially the section on https://packaging.python.org/en/latest/guides/distributing-packages-using-setuptools/#setup-args[setup args]. +//// +$ curl https://codeberg.org/mhwombat/hello-flake-python/raw/branch/main/setup.py --silent --output setup.py +//// + +[source,python,linenums] +.setup.py .... -$# curl https://codeberg.org/mhwombat/hello-flake-python/raw/branch/main/setup.py --silent --output setup.py -$ cat setup.py +$# cat setup.py .... We won’t write `flake.nix` just yet. First we’ll try building the @@ -143,9 +157,14 @@ function. If you put all the pieces together, your `flake.nix` should look something like this. +//// +$ curl https://codeberg.org/mhwombat/hello-flake-python/raw/branch/main/flake.nix --silent --output flake.nix +//// + +[source,nix,linenums] +.flake.nix .... -$# curl https://codeberg.org/mhwombat/hello-flake-python/raw/branch/main/flake.nix --silent --output flake.nix -$ cat flake.nix +$# cat flake.nix .... Let’s try out the new flake.