devshell/getting_started.html
2024-07-27 20:51:04 +00:00

389 lines
20 KiB
HTML

<!DOCTYPE HTML>
<html lang="en" class="light" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Getting started - devshell</title>
<!-- Custom HTML head -->
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="favicon.svg">
<link rel="shortcut icon" href="favicon.png">
<link rel="stylesheet" href="css/variables.css">
<link rel="stylesheet" href="css/general.css">
<link rel="stylesheet" href="css/chrome.css">
<link rel="stylesheet" href="css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="highlight.css">
<link rel="stylesheet" href="tomorrow-night.css">
<link rel="stylesheet" href="ayu-highlight.css">
<!-- Custom theme stylesheets -->
<link rel="stylesheet" href="theme/pagetoc.css">
</head>
<body class="sidebar-visible no-js">
<div id="body-container">
<!-- Provide site root to javascript -->
<script>
var path_to_root = "";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
var html = document.querySelector('html');
html.classList.remove('light')
html.classList.add(theme);
var body = document.querySelector('body');
body.classList.remove('no-js')
body.classList.add('js');
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var body = document.querySelector('body');
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
body.classList.remove('sidebar-visible');
body.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<div class="sidebar-scrollbox">
<ol class="chapter"><li class="chapter-item expanded "><a href="intro.html"><strong aria-hidden="true">1.</strong> Intro</a></li><li class="chapter-item expanded "><a href="getting_started.html" class="active"><strong aria-hidden="true">2.</strong> Getting started</a></li><li class="chapter-item expanded "><a href="ci.html"><strong aria-hidden="true">3.</strong> Continuous Integration setup</a></li><li class="chapter-item expanded "><a href="extending.html"><strong aria-hidden="true">4.</strong> Extending devshell</a></li><li class="chapter-item expanded "><a href="modules_schema.html"><strong aria-hidden="true">5.</strong> devshell.toml schema</a></li><li class="chapter-item expanded "><a href="env.html"><strong aria-hidden="true">6.</strong> env vars</a></li><li class="chapter-item expanded "><a href="99_todo.html"><strong aria-hidden="true">7.</strong> TODO</a></li></ol>
</div>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<!-- Track and set sidebar scroll position -->
<script>
var sidebarScrollbox = document.querySelector('#sidebar .sidebar-scrollbox');
sidebarScrollbox.addEventListener('click', function(e) {
if (e.target.tagName === 'A') {
sessionStorage.setItem('sidebar-scroll', sidebarScrollbox.scrollTop);
}
}, { passive: true });
var sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
sessionStorage.removeItem('sidebar-scroll');
if (sidebarScrollTop) {
// preserve sidebar scroll position when navigating via links within sidebar
sidebarScrollbox.scrollTop = sidebarScrollTop;
} else {
// scroll sidebar to current active section when navigating via "next/previous chapter" buttons
var activeSection = document.querySelector('#sidebar .active');
if (activeSection) {
activeSection.scrollIntoView({ block: 'center' });
}
}
</script>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">devshell</h1>
<div class="right-buttons">
<a href="print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/numtide/devshell" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
<a href="https://github.com/numtide/devshell/edit/main/docs/src/getting_started.md" title="Suggest an edit" aria-label="Suggest an edit">
<i id="git-edit-button" class="fa fa-edit"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<div class="content-wrap">
<h1 id="getting-started"><a class="header" href="#getting-started">Getting started</a></h1>
<p>This project has a single dependency; Nix. It will be used to pull in all
other dependencies. It can be installed by following the instructions
over there: https://nixos.org/download.html#nix-quick-install</p>
<p>Now that's done, got to your project root and create an empty <code>devshell.toml</code>.</p>
<p>There are different ways to load that config depending on your preferences:</p>
<h3 id="nix-non-flake"><a class="header" href="#nix-non-flake">Nix (non-flake)</a></h3>
<p>Add another file called <code>shell.nix</code> with the following content. This file will
contain some nix code. Don't worry about the details.</p>
<pre><code class="language-nix">{ system ? builtins.currentSystem }:
let
src = fetchTarball "https://github.com/numtide/devshell/archive/main.tar.gz";
devshell = import src { inherit system; };
in
devshell.fromTOML ./devshell.toml
</code></pre>
<blockquote>
<p>NOTE: it's probably a good idea to pin the dependency by replacing <code>main</code> with a git commit ID.</p>
</blockquote>
<p>Now you can enter the developer shell for the project:</p>
<pre><code class="language-console">$ nix-shell
warning: you did not specify '--add-root'; the result might be removed by the garbage collector
these 4 derivations will be built:
/nix/store/nvbfq9h68r63k5jkfnbimny3b35sx0fs-devshell-bashrc.drv
/nix/store/ppfyf9zv023an8477hcbjlj0rbyvmwq7-devshell.env.drv
/nix/store/8027cgy3xcinb59aaynh899q953dnzms-devshell-bin.drv
/nix/store/w33zl180ni880p18sls5ykih88zkmkqk-devshell.drv
building '/nix/store/nvbfq9h68r63k5jkfnbimny3b35sx0fs-devshell-bashrc.drv'...
building '/nix/store/ppfyf9zv023an8477hcbjlj0rbyvmwq7-devshell-env.drv'...
created 1 symlinks in user environment
building '/nix/store/8027cgy3xcinb59aaynh899q953dnzms-devshell-bin.drv'...
building '/nix/store/w33zl180ni880p18sls5ykih88zkmkqk-devshell.drv'...
warning: you did not specify '--add-root'; the result might be removed by the garbage collector
🔨 Welcome to devshell
[general commands]
menu - prints this menu
[devshell]$
</code></pre>
<h3 id="nix-flakes"><a class="header" href="#nix-flakes">Nix Flakes</a></h3>
<p>For users of nix flakes, a default template is provided to get you up and
running.</p>
<pre><code class="language-sh">nix flake new -t "github:numtide/devshell" project/
cd project/
# enter the shell
nix develop # or `direnv allow` if you want to use direnv
</code></pre>
<p>Find <code>templates/gettingStartedExample</code> in this repository for a working example of the additional configuration below: <code>env</code>, <code>packages</code>, and <code>serviceGroups</code>.</p>
<h2 id="adding-environment-variables"><a class="header" href="#adding-environment-variables">Adding environment variables</a></h2>
<p>Environment variables that are specific to the project can be added with the
<code>[[env]]</code> declaration. Each environment variable is an entry in an array, and
will be set in the order that they are declared.</p>
<p>Eg:</p>
<pre><code class="language-toml">[[env]]
name = "GO111MODULE"
value = "on"
</code></pre>
<p>There are different ways to set the environment variables. Look at the schema
to find all the ways. But in short:</p>
<ul>
<li>Use the <code>value</code> key to set a fixed env var.</li>
<li>Use the <code>eval</code> key to evaluate the value. This is useful when one env var
depends on the value of another.</li>
<li>Use the <code>prefix</code> key to prepend a path to an environment variable that uses
the path separator. Like <code>PATH</code>.</li>
</ul>
<h2 id="adding-new-commands"><a class="header" href="#adding-new-commands">Adding new commands</a></h2>
<p>Devshell also supports adding new commands to the environment. Those are
displayed on devshell entry so that the user knows what commands are available
to them.</p>
<p>In order to bring in new dependencies, you can either add them to
<code>devshell.packages</code> or as an entry in the <code>[[commands]]</code> list (see <a href="https://toml.io/en/v1.0.0#array-of-tables">TOML docs</a>). Commands are also added to the
menu so they might be preferable for discoverability.</p>
<p>As an exercise, add the following snippet to your <code>devshell.toml</code>:</p>
<pre><code class="language-toml">[[commands]]
package = "go"
</code></pre>
<p>Then re-enter the shell with <code>nix-shell</code>/<code>nix develop</code>/<code>direnv reload</code>. You should see something like this:</p>
<pre><code class="language-console">$ nix-shell
warning: you did not specify '--add-root'; the result might be removed by the garbage collector
these 4 derivations will be built:
/nix/store/nvbfq9h68r63k5jkfnbimny3b35sx0fs-devshell-bashrc.drv
/nix/store/ppfyf9zv023an8477hcbjlj0rbyvmwq7-devshell.env.drv
/nix/store/8027cgy3xcinb59aaynh899q953dnzms-devshell-bin.drv
/nix/store/w33zl180ni880p18sls5ykih88zkmkqk-devshell.drv
building '/nix/store/nvbfq9h68r63k5jkfnbimny3b35sx0fs-devshell-bashrc.drv'...
building '/nix/store/ppfyf9zv023an8477hcbjlj0rbyvmwq7-devshell-env.drv'...
created 1 symlinks in user environment
building '/nix/store/8027cgy3xcinb59aaynh899q953dnzms-devshell-bin.drv'...
building '/nix/store/w33zl180ni880p18sls5ykih88zkmkqk-devshell.drv'...
warning: you did not specify '--add-root'; the result might be removed by the garbage collector
🔨 Welcome to devshell
[general commands]
menu - prints this menu
go - The Go Programming language
[devshell]$
</code></pre>
<p>Now the <code>go</code> program is available in the environment and can be used to
develop Go programs. This can easily be adapted to any language.</p>
<p>Similarly, you could also add go to the packages list, in which case it would
not appear in the menu:</p>
<pre><code class="language-toml">[devshell]
packages = [
"go"
]
</code></pre>
<h3 id="finding-packages"><a class="header" href="#finding-packages">Finding packages</a></h3>
<p>Check out the <a href="https://search.nixos.org/packages">Nix package repository</a>.</p>
<p>Note that it is also possible to <strong>use specific versions</strong> for some packages - e.g. for NodeJS, <a href="https://search.nixos.org/packages?type=packages&amp;query=nodejs">search the repo</a> &amp; use like this:</p>
<pre><code class="language-toml">[[commands]]
package = "nodejs-17_x" # https://search.nixos.org/packages?type=packages&amp;query=nodejs
name = "node"
help = "NodeJS"
</code></pre>
<p>Or another example:</p>
<pre><code class="language-toml">[devshell]
packages = [
"python27", # 2.7
"python311", # 3.11
]
</code></pre>
<h2 id="adding-background-services"><a class="header" href="#adding-background-services">Adding background services</a></h2>
<p>Many projects need background services to be running during development or to
run tests (e.g. a database, caching server, webserver, ...). This is supported
in devshell through the concept of service groups.</p>
<p>A service group defines a collection of long-running processes that can be
started and stopped.</p>
<p>An example service group could be configured like this:</p>
<pre><code class="language-toml">[serviceGroups.database]
description = "Runs a database in the backgroup"
[serviceGroups.database.services.postgres]
command = "postgres"
[serviceGroups.database.services.memcached]
command = "memcached"
</code></pre>
<p>This will add two commands to the devshell: <code>database:start</code> and
<code>database:stop</code>. <code>database:start</code> starts the defined services in the <code>database</code>
group in the foreground and shows their output. <code>database:stop</code> can be executed
in a different shell to stop the processes (or press Ctrl-C in the main shell).</p>
<h2 id="wrapping-up"><a class="header" href="#wrapping-up">Wrapping up</a></h2>
<p><strong>devshell</strong> is extensible in many different ways. In the next chapters we will
discuss the various ways in which it can be adapted to your project's needs.</p>
</div>
<div class="sidetoc">
<nav class="pagetoc"></nav>
</div>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="intro.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="ci.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="intro.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="ci.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<script>
window.playground_copyable = true;
</script>
<script src="elasticlunr.min.js"></script>
<script src="mark.min.js"></script>
<script src="searcher.js"></script>
<script src="clipboard.min.js"></script>
<script src="highlight.js"></script>
<script src="book.js"></script>
<!-- Custom JS scripts -->
<script src="theme/pagetoc.js"></script>
</div>
</body>
</html>