-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
749 lines (529 loc) · 15.7 KB
/
index.html
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Declarative configuration with NixOS</title>
<meta name="description" content="">
<meta name="author" content="David Sferruzza">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match(/print-pdf/gi) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName('head')[0].appendChild(link);
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section data-markdown data-separator="^\r?\n---\r?\n$">
<textarea data-template>
# Declarative configuration with NixOS
## David Sferruzza
### 2018-07-01
---
# About me
- [@d_sferruzza](https://twitter.com/d_sferruzza)
- [github.com/dsferruzza](https://github.com/dsferruzza)
- PhD student in software engineering at *LS2N*
- Work at [Startup Palace](https://www.startup-palace.com)
![](img/sp.gif) <!-- .element: class="stretch" -->
---
# Startup Palace
We **iteratively** design and develop web applications. <small>(among other things)</small>
Frequent reviews (by customers and team members) are an important part of the job.
> We need staging/preprod environments
![](img/crash.gif) <!-- .element: class="stretch" -->
---
# Staging environments
They should be:
- quick to create
- quick to configure
- quick to remove
- cheap
- compatible with automatic deployment
![](img/cups.gif) <!-- .element: class="stretch" -->
Note: This shoud be a **convenience**.
---
# About automatic deployment
We use [GitLab](https://about.gitlab.com/)
![](img/gitlab.svg) <!-- .element: class="stretch" -->
> GitLab unifies chat, issues, code review,<br>CI and CD into a single UI
---
# About automatic deployment
GitLab CI is very flexible:
```yaml
my_deploy:
stage: deploy
image: org/my_docker_image
script:
- echo "I can do anything here"
- rsync ./my_projet $USER@$HOST:/var/www/
only:
- master
tags:
- docker
```
---
# Naive solution
We know [Debian](https://www.debian.org/). Let's use Debian!
And let's install **and** *configure*:
- Apache
- PHP
- PostgreSQL
- SSH
*by hand*!
![](img/madness.gif) <!-- .element: class="stretch" -->
---
# Naive solution
It works but:
- it needs a lot of manual setup<br>→ admins need to be watchful and rigorous
- environments can diverge<br>→ it can quickly become a mess
- security/system updates can break stuff<br>→ you need to fix environments one by one
- it's soooo **boring** to use
> Boring means I will make mistakes
---
# Naive solution
We did it for a while.
*1 star. Would not recommend.*
![](img/rain.gif) <!-- .element: class="stretch" -->
So, can we do better?
---
# Dockerfile
At that time, people were talking a lot about Docker. So I took a look.
The concept of the **Dockerfile** is interesting:
- you start from an existing image
- you *declare* how to change it
- you get a new image
```dockerfile
FROM debian:jessie
RUN apt-get update \
&& apt-get install --no-install-recommends -y \
openjdk-7-jdk
```
---
# Dockerfile
This is fine. But:
- you declare **how** to build an image<br>not **what** is the image you want to build
- this is too linear (layers of cache)
- it's hard to compose
- ...
![](img/this-is-fine.jpg) <!-- .element: class="stretch" -->
---
# Declarative vs. imperative
> **Imperative programming**: use statements that change a program's state
>
> **Declarative programming**: express the logic of a computation without describing its control flow
- imperative: shopping instructions
- declarative: shopping list
---
# The NixOS project
> <figure class="stretch"><img src="img/nixos.svg" alt=""></figure>
> <https://nixos.org/>
- Nix
- Nix Expression Language
- Nixpkgs
- NixOS (Linux distribution)
- ...
Note: Created by *Eelco Dolstra* for its PhD
---
# Nix
> The Purely Functional Package Manager
>
> <https://nixos.org/nix/>
Packages:
- are treated like values
- are built by functions that don't have side-effects
- never change after they have been built
Builds are **reproducible**.
---
# Nix store
The place where packages are stored. Usually in `/nix/store/`
Example:<br><small>`/nix/store/f4gxsj6pn4ygqadwyk2m6xg1ywhfwxg1-openssl-1.0.2h/`</small>
A package's directory name contains:
- its name
- its version
- a unique identifier that captures all its dependencies
Note: unique identifier = a cryptographic hash of the package’s build dependency graph
---
# The `which` package
```nix
{ stdenv, fetchurl }:
stdenv.mkDerivation rec {
name = "which-2.21";
src = fetchurl {
url = "mirror://gnu/which/${name}.tar.gz";
sha256 = "1bgafvy3ypbhhfznwjv1lxmd6mci3x1byilnnkc7gcr486wlb8pl";
};
meta = with stdenv.lib; {
homepage = http://ftp.gnu.org/gnu/which/;
platforms = platforms.all;
license = licenses.gpl3;
};
}
```
<!-- .element: class="smallcode" -->
```text
result/
├── bin
│ └── which
└── share
├── info
│ └── which.info
└── man
└── man1
└── which.1.gz
```
<!-- .element: class="smallcode" -->
---
# The `which` package
```nix
{ stdenv, fetchurl }:
stdenv.mkDerivation rec {
name = "which-2.21";
src = fetchurl {
url = "mirror://gnu/which/${name}.tar.gz";
sha256 = "1bgafvy3ypbhhfznwjv1lxmd6mci3x1byilnnkc7gcr486wlb8pl";
};
meta = with stdenv.lib; {
homepage = http://ftp.gnu.org/gnu/which/;
platforms = platforms.all;
license = licenses.gpl3;
};
}
```
<!-- .element: class="smallcode" -->
```text
$ ldd result/bin/which
linux-gate.so.1 (0xb778e000)
libc.so.6 => /nix/store/6nqfrgw8w5xmm0zarp8f51rvs9lvgq73-glibc-2.23/lib/libc.so.6 (0xb75ea000)
/nix/store/6nqfrgw8w5xmm0zarp8f51rvs9lvgq73-glibc-2.23/lib/ld-linux.so.2 (0xb7790000)
```
<!-- .element: class="smallcode" -->
Note: ldd - print shared library dependencies
---
# Nix
Nice features:
- multiple versions
- complete (and explicit) dependencies
- multi-user
- atomic upgrades and rollbacks
- transparent source/binary deployment
![](img/nice.gif) <!-- .element: class="stretch" -->
Note: build & runtime dependencies
---
# User environments
![](img/user-envs.png) <!-- .element: class="stretch" -->
---
# Nix Expression Language
> It is a pure, lazy, functional language.
>
> It is **not** a general purpose language.
Types of expressions:
- strings, integers, paths, booleans, null
- functions/lambdas
- lists
- sets
- derivations (package build actions)
---
# Nix Expression Language
```nix
# This is a list of strings
[ "a" "b" ''c'' "var=${var}" ]
# This is a lambda
f = x: x * x
# This is a set
{ a = 5; b = f 2; }
# This is a recursive set
rec { a = b + 1; b = 5; }
```
---
# Nixpkgs
> A collection of packages for the Nix package manager.
>
> <https://github.com/NixOS/nixpkgs>
- has nearly 43,000 packages
- uses a permissive MIT/X11 license
- includes a [standard library](https://github.com/NixOS/nixpkgs/tree/master/lib) for Nix Expression Language
---
# The `which` package
```nix
{ stdenv, fetchurl }:
stdenv.mkDerivation rec {
name = "which-2.21";
src = fetchurl {
url = "mirror://gnu/which/${name}.tar.gz";
sha256 = "1bgafvy3ypbhhfznwjv1lxmd6mci3x1byilnnkc7gcr486wlb8pl";
};
meta = with stdenv.lib; {
homepage = http://ftp.gnu.org/gnu/which/;
platforms = platforms.all;
license = licenses.gpl3;
};
}
```
<!-- .element: class="smallcode" -->
---
# NixOS
The Purely Functional Linux Distribution
> Nix + Nixpkgs + systemd + Linux kernel
- declarative system configuration model
- reliable upgrades
- atomic upgrades
- rollbacks
- reproducible system configurations
- safe to test changes
- Nix goodness (multi-user package management, source-based model with binaries, consistency, ...)
Note: reliable = installing & upgrading is like from scratch
---
# NixOS
```nix
{ config, lib, pkgs, ... }:
{
boot.loader.grub.device = "/dev/sda";
fileSystems."/".device = "/dev/sda1";
networking.firewall = {
enable = true;
allowedTCPPorts = [ 80 ];
};
environment.systemPackages = with pkgs; [
wget
git
];
services = {
sshd.enable = true;
munin-node.enable = true;
munin-cron = {
enable = true;
hosts = ''
[${config.networking.hostName}]
address localhost
'';
};
};
}
```
<!-- .element: class="smallcode" -->
---
# NixOS
Realize the configuration:
```text
$ nixos-rebuild switch
```
Rollback to the previous configuration:
```text
$ nixos-rebuild switch --rollback
```
Only build configuration:
```text
$ nixos-rebuild build
```
Test configuration:
```text
$ nixos-rebuild test
$ nixos-rebuild build-vm
```
---
# NixOS
Build your own system config using:
- the `/etc/nixos/configuration.nix` file
- a functional programming language
- a module system
- packages and generic functions from Nixpkgs
![](img/lego.gif) <!-- .element: class="stretch" -->
---
# Options
> A NixOS module can:
>
> - declare options
> - define options depending on other options' values
Built-in options: <https://nixos.org/nixos/options.html>
**Example**: I want a boolean option `myCustomBashAliases` that defines some Bash aliases when enabled.
---
# Options
In `/etc/nixos/myModule.nix`:
```nix
{ config, lib, pkgs, ... }:
{
options = {
myCustomBashAliases = lib.mkOption {
default = false;
description = "Enable my awesome Bash aliases.";
types = lib.types.bool;
};
};
config = lib.mkIf config.myCustomBashAliases {
programs.bash.shellAliases = {
ls = "ls --color=auto";
ll = "ls --color=auto -lh";
l = "ls --color=auto -lha";
};
};
}
```
<!-- .element: class="smallcode" -->
---
# Options usage
In `/etc/nixos/configuration.nix`:
```nix
{ config, pkgs, ... }:
{
imports =
[
# ...
./myModule.nix
];
myCustomBashAliases = true;
# ...
}
```
<!-- .element: class="smallcode" -->
This allows to build very powerful abstractions!
---
# Back to the problem
I want to be able to create staging environments:
- quickly
- easily
- in a reliable way
→ Let's write some NixOS modules!
![](img/lets-do-this.gif) <!-- .element: class="stretch" -->
---
# Knights Of Nix
An abstraction over NixOS to manage staging environments for static and PHP web apps.
![](img/ni.gif) <!-- .element: class="stretch" -->
Disclaimers:
- This is a **Proof of Concept**
- This is **not** a *Platform as a Service* (PaaS)
---
# Knights Of Nix
High-level definition of staging environments:
```nix
[
# Here is a fake app
rec {
appType = "php";
phpVersion = "70";
name = "test";
hostName = "${name}.mydomain.com";
userSha512Password = "...";
pgClearPassword = "...";
}
]
```
---
# Knights Of Nix
Nice features:
- multi-domain support (with redirections)
- HTTPS support (automatic certification generation with [Let's Encrypt](https://letsencrypt.org/))
- per-app user, with SSH access (I can auto-deploy from GitLab \\o/)
- per-app PHP version
- per-app Cron jobs
![](img/combo.gif) <!-- .element: class="stretch" -->
---
# Bonus: nix-shell
Drop a Bash shell configured with the same environment that would be used to perform the build itself.
```text
$ nix-shell '<nixpkgs>' -A nodejs-5_x
these paths will be fetched (21.40 MiB download, 21.41 MiB unpacked):
/nix/store/mbv89fkb9av98alqfaay8c4n3czk82q9-node-v5.9.0.tar.gz
fetching path ‘/nix/store/mbv89fkb9av98alqfaay8c4n3czk82q9-node-v5.9.0.tar.gz’...
*** Downloading ‘https://cache.nixos.org/nar/0qzzksfad1g8kk3rfpdiyhdp9h5737nn70zywakarc2lf3qhg0g1.nar.xz’ (signed by ‘cache.nixos.org-1’) to ‘/nix/store/mbv89fkb9av98alqfaay8c4n3czk82q9-node-v5.9.0.tar.gz’...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 21.4M 100 21.4M 0 0 1173k 0 0:00:18 0:00:18 --:--:-- 1374k
[nix-shell:~]$
```
<!-- .element: class="smallcode" -->
---
# Bonus: package customization
```nix
nixpkgs.config.packageOverrides = pkgs: rec {
simp_le = pkgs.simp_le.overrideDerivation (oldAttrs: {
patches = [
(pkgs.fetchpatch {
url = "https://github.com/kuba/simp_le/commit/" +
"4bc788fdd611c4118c3f86b5f546779723aca5a7.patch";
sha256 = "0036p11qn3plydv5s5z6i28r6ihy1ipjl0y8la0izpkiq273byfc";
})
];
});
};
```
<!-- .element: class="smallcode" -->
![](img/mind-blown.gif) <!-- .element: class="stretch" -->
Note: completely override package, not only the installed instance
---
# Conclusion
Nix & NixOS have very interesting properties that can make server management **easier** and **safer**.
You can use software practices to do sysadmin: modularity, DRY, versioning, abstractions, ...
> The NixOS project is still a bit young, but it grows and you should definitely take a look!
>
> → <https://nixos.org>
---
# References
- [NixOS manual](https://nixos.org/nixos/manual/)
- [NixOS options](http://nixos.org/nixos/options.html) & [NixOS packages](http://nixos.org/nixos/packages.html)
- [Nix manual](https://nixos.org/nix/manual/)
- [Nixpkgs source code](https://github.com/NixOS/nixpkgs)
- [Nix pills](https://lethalman.blogspot.fr/search/label/nixpills)
- [Nix/NixOS papers](https://nixos.org/~eelco/pubs/)
![](img/reading.gif) <!-- .element: class="stretch" -->
---
# Questions?
![](img/question.gif) <!-- .element: class="stretch" -->
Twitter: [@d_sferruzza](https://twitter.com/d_sferruzza)
Slides on GitHub:
[dsferruzza/conf-declarative-configuration-with-nixos](http://github.com/dsferruzza/conf-declarative-configuration-with-nixos)
</textarea>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script src="plugin/markdown/marked.js"></script>
<script>
function paragraphRenderer(text) {
var singleImage = text.match(/^(<img src=".+" alt=".*">)(\s*<!--.+-->)?$/);
if (singleImage !== null) {
var stretchComment = (typeof singleImage[2] === 'string') ? singleImage[2].match(/^\s*<!--\s*\.element:\s*(.*?)\s*(class\s*=\s*"stretch")\s*(.*?)\s*-->$/) : null;
if (stretchComment !== null && typeof stretchComment[2] === 'string') {
var comment = '<!-- .element: ' + ((typeof stretchComment[1] === 'string') ? stretchComment[1] : '') + ' ' + ((typeof stretchComment[3] === 'string') ? stretchComment[3] : '') + ' -->';
return '<figure class="stretch">' + singleImage[1] + comment + '</figure>';
}
else {
var comment = (typeof singleImage[2] === 'string') ? singleImage[2] : '';
return '<figure>' + singleImage[1] + comment + '</figure>';
}
}
else {
return '<p>' + text + '</p>\n';
}
}
var patchedRenderer = new marked.Renderer();
patchedRenderer.paragraph = paragraphRenderer;
Reveal.initialize({
controls: false,
slideNumber: true,
history: true,
transition: 'none',
dependencies: [
// { src: 'plugin/markdown/marked.js' }, // <-- useless because included before
{ src: 'plugin/markdown/markdown.js' },
{ src: 'plugin/notes/notes.js', async: true },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true },
],
markdown: {
renderer: patchedRenderer,
},
});
</script>
</body>
</html>