From 5176c69d24ca2971cedddb3d438a931ed0206643 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amy=20de=20Buitl=C3=A9ir?= Date: Sat, 18 Nov 2023 14:21:55 +0000 Subject: [PATCH] added recipes --- index.html | 129 +++++++++++++++++++++++++++++++-- source/shebangs/main.adoc | 4 +- source/shell-recipes/main.adoc | 2 + 3 files changed, 128 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index 2be7afd..aa79ceb 100644 --- a/index.html +++ b/index.html @@ -94,7 +94,7 @@ pre.pygments .tok-il { color: #666666 } /* Literal.Number.Integer.Long */
-

9.6. Shell with an environment variable set

+

9.6. Shell with runtime access to a Haskell library on your local computer (no .cabal file required)

+
+

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. +This shell provides access to the Haskell containers library (package).

+
+
+
shell.nix
+
+
1
+2
+3
+4
+5
+6
+7
+8
+9
with (import <nixpkgs> {});
+let
+  customGhc = haskellPackages.ghcWithPackages (pkgs: with pkgs; [ containers ]);
+in
+mkShell {
+  buildInputs = [
+                  customGhc
+                ];
+}
+
+
+
+
+

Here’s a short Haskell program that uses it.

+
+
+
Main.hs
+
+
 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+10
+11
import Data.Map
+
+m :: Map String Int
+m = fromList [("cats", 3), ("dogs", 2)]
+
+main :: IO ()
+main = do
+  let cats = findWithDefault 0 "cats" m
+  let dogs = findWithDefault 0 "dogs" m
+  let zebras = findWithDefault 0 "zebras" m
+  print $ "I have " ++ show cats ++ " cats, " ++ show dogs ++ " dogs, and " ++ show zebras ++ " zebras."
+
+
+
+
+

Here’s a demonstration using the program.

+
+
+
+
$ nix-shell
+$ runghc Main.hs
+"I have 3 cats, 2 dogs, and 0 zebras."
+
+
+
+
+

9.7. Shell with an environment variable set

This shell has the environment variable FOO set to “bar”

@@ -2101,7 +2174,7 @@ separate flake.nix.

The script should start with two “shebang” (#!) commands. The first -should invoke nix-shell. The second should declares the scrpt +should invoke nix-shell. The second should declares the script interpreter and any dependencies. Here are some examples.

@@ -2132,7 +2205,51 @@ cowsay "Pretty cool, huh?&q
-

10.2. A Python script depending on a package in the nixpkgs repo.

+

10.2. A Haskell script depending on a Haskell library package in the nixpkgs repo.

+
+
Script
+
+
 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+10
+11
+12
+13
+14
+15
#! /usr/bin/env nix-shell
+#! nix-shell -p "haskellPackages.ghcWithPackages (p: [p.containers])"
+#! nix-shell -i runghc
+
+import Data.Map
+
+m :: Map String Int
+m = fromList [("cats", 3), ("dogs", 2)]
+
+main :: IO ()
+main = do
+  let cats = findWithDefault 0 "cats" m
+  let dogs = findWithDefault 0 "dogs" m
+  let zebras = findWithDefault 0 "zebras" m
+  print $ "I have " ++ show cats ++ " cats, " ++ show dogs ++ " dogs, and " ++ show zebras ++ " zebras."
+
+
+
+
+
Output
+
+
"I have 3 cats, 2 dogs, and 0 zebras."
+
+
+
+
+

10.3. A Python script depending on a package in the nixpkgs repo.

Script
diff --git a/source/shebangs/main.adoc b/source/shebangs/main.adoc index 1b11096..f862940 100644 --- a/source/shebangs/main.adoc +++ b/source/shebangs/main.adoc @@ -6,9 +6,11 @@ standalone scripts because you don’t need to create a repo and write a separate `flake.nix`. The script should start with two "`shebang`" (`#!`) commands. The first -should invoke `nix-shell`. The second should declares the scrpt +should invoke `nix-shell`. The second should declares the script interpreter and any dependencies. Here are some examples. include::bash-with-nix-pkg/main-generated.adoc[leveloffset=+1] +include::haskell-with-nix-pkg/main-generated.adoc[leveloffset=+1] + include::python-with-nix-pkg/main-generated.adoc[leveloffset=+1] diff --git a/source/shell-recipes/main.adoc b/source/shell-recipes/main.adoc index a1c7976..cf28d0f 100644 --- a/source/shell-recipes/main.adoc +++ b/source/shell-recipes/main.adoc @@ -16,4 +16,6 @@ include::shell-haskell-local/main-generated.adoc[leveloffset=+1] include::shell-haskell-local-deps/main-generated.adoc[leveloffset=+1] +include::shell-haskell-no-cabal/main-generated.adoc[leveloffset=+1] + include::shell-with-env-var/main-generated.adoc[leveloffset=+1]