|
| 1 | +*[todo: We need to put together a short style guide for consistency, including* |
| 2 | +* Nix (not nix) |
| 3 | +* NixOS (not NixOs etc.) |
| 4 | +* Should we say "folder" or "directory"? Younger people seem to prefer "folder" ] |
| 5 | + |
| 6 | +# Installing Blueprint |
| 7 | + |
| 8 | +Let's create a small project with Nix, and you'll see how to add Blueprint to your project. |
| 9 | + |
| 10 | +1. Install [Nix](https://nix.dev/install-nix). |
| 11 | +2. Run `mkdir my-project && cd my-project` |
| 12 | +3. Run `nix flake init -t github:numtide/blueprint`. |
| 13 | + |
| 14 | +Note: After you install Nix, you'll need to enable "experimental features." Find out how here. |
| 15 | + |
| 16 | +This will give you a barebone project structure with a single `flake.nix` file and a single `devshell.nix` file. (It also provides a basic .envrc, which [TODO] and a starter .gitignore file. Make sure you're aware of this .gitignore file before you accidentally overwrite it.) |
| 17 | + |
| 18 | +Normally, without Blueprint, you would typically include a devShell section inside your flake.nix file. In that scenario, when you want to start a new project with a similar toolset, you'll likely need to copy over the devShell section of your flake.nix file to the new project's flake.nix file. But by using Blueprint, we've split out the devShell into its own file, allowing you to simply copy the file over. |
| 19 | + |
| 20 | +Here's the starting point of your devShell.nix file: |
| 21 | + |
| 22 | +```nix |
| 23 | +{ pkgs }: |
| 24 | +pkgs.mkShell { |
| 25 | + # Add build dependencies |
| 26 | + packages = [ ]; |
| 27 | +
|
| 28 | + # Add environment variables |
| 29 | + env = { }; |
| 30 | +
|
| 31 | + # Load custom bash code |
| 32 | + shellHook = '' |
| 33 | +
|
| 34 | + ''; |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +In a moment we'll look at what you can do with this file. Meanwhile, here's the flake.nix file: |
| 39 | + |
| 40 | +``` |
| 41 | +{ |
| 42 | + description = "Simple flake with a devshell"; |
| 43 | +
|
| 44 | + # Add all your dependencies here |
| 45 | + inputs = { |
| 46 | + nixpkgs.url = "github:NixOS/nixpkgs?ref=nixos-unstable"; |
| 47 | + blueprint.url = "github:numtide/blueprint"; |
| 48 | + blueprint.inputs.nixpkgs.follows = "nixpkgs"; |
| 49 | + }; |
| 50 | +
|
| 51 | + # Load the blueprint |
| 52 | + outputs = inputs: inputs.blueprint { inherit inputs; }; |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +You generally shouldn't have to modify this file (unless you're adding new inputs). |
| 57 | + |
| 58 | +When you run a nix command (such as `nix develop`), this flake.nix file is evaluated and Nix loads the Blueprint into the Nix store and makes it available to your current session. Blueprint in turn allows you to read .nix files from multiple locations within your project, including: |
| 59 | + |
| 60 | +* The devShell.nix file in the root of your project |
| 61 | +* A folder structure |
| 62 | + |
| 63 | +You create the folder structure based on the available list of folders (found here). |
| 64 | + |
| 65 | +# A Sample Environment |
| 66 | + |
| 67 | +Let's set up a development environment that includes: |
| 68 | + |
| 69 | +* Python |
| 70 | +* Python's numpy package |
| 71 | + |
| 72 | +Open up the devshell.nix file in your favorite editor, and update it to look like this: |
| 73 | + |
| 74 | +```nix |
| 75 | +{ pkgs }: |
| 76 | +pkgs.mkShell { |
| 77 | + # Add build dependencies |
| 78 | + packages = [ |
| 79 | + pkgs.python3 |
| 80 | + pkgs.python3Packages.numpy |
| 81 | + ]; |
| 82 | +
|
| 83 | + # Add environment variables |
| 84 | + env = { }; |
| 85 | +
|
| 86 | + # Load custom bash code |
| 87 | + shellHook = '' |
| 88 | + export PS1="(python numpy) $PS1" |
| 89 | + ''; |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +Notice we added two packages, Python and the NumPy Python package. |
| 94 | + |
| 95 | +We also added a line inside shellHook. (This line is not required, but it's handy, as it updates the prompt to let you know when you're inside a nix shell.) |
| 96 | + |
| 97 | +Now let's hop into the developer shell by typing: |
| 98 | + |
| 99 | +```bash |
| 100 | +nix develop |
| 101 | +``` |
| 102 | + |
| 103 | +After a short moment where Nix downloads the packages, you'll be inside the shell. To verify the packages were installed, type: |
| 104 | + |
| 105 | +```bash |
| 106 | +python |
| 107 | +``` |
| 108 | + |
| 109 | +Then, inside python type: |
| 110 | + |
| 111 | +``` |
| 112 | +import numpy |
| 113 | +``` |
| 114 | + |
| 115 | +You shouldn't see any error. |
| 116 | + |
| 117 | +That's it; go ahead and exit python by typing |
| 118 | + |
| 119 | +```bash |
| 120 | +quit() |
| 121 | +``` |
| 122 | + |
| 123 | +When you're ready to exit the development shell, you can simply type: |
| 124 | + |
| 125 | +```bash |
| 126 | +exit |
| 127 | +``` |
| 128 | + |
| 129 | +## What did this demonstrate? |
| 130 | + |
| 131 | +The above demonstrated that the devshell.nix file is now self-contained and can be used without having to add devshell code inside your flake.nix file. |
| 132 | + |
| 133 | +There's much more, however, that you can do. |
| 134 | + |
| 135 | +Check out: |
| 136 | + |
| 137 | +* Examples (including the rest of our Python/NumPy example) |
| 138 | +* Guides |
| 139 | +* Contributing |
| 140 | + |
| 141 | +# (Optional) Configuring direnv |
| 142 | + |
| 143 | +Included in the initial files created by Blueprint is a filed called .envrc. This file contains code to configure direnv, which allows you to enter a devshell simply by switching to the folder containing your project. That means you don't need to type `nix develop` after entering the folder. Then when you move up and out of the folder, you'll automatically exit the environment. |
| 144 | + |
| 145 | +For more information on configuring this feature, check out our guide at [Configuring Direnv](../guides/configuring_direnv.md) |
| 146 | + |
| 147 | + |
| 148 | +## Creating a root folder |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | +## Adding a host |
| 155 | + |
| 156 | +TODO |
| 157 | + |
| 158 | +## Adding a package |
| 159 | + |
| 160 | +TODO |
0 commit comments