Functional Programming and Nix for Reproducible, Immutable Infrastructure
Total Page:16
File Type:pdf, Size:1020Kb
Functional programming and Nix for reproducible, immutable infrastructure Nix Functions and packaging Agenda Functions and operating systems Development shells Docker without Dockerfiles Nix Functional programming for infrastructure Nix History Functions: same input, same output 12th century “Nix: A Safe and Policy-Free System for Software History Deployment” Eelco Dolstra 2004 “NixOS: A Purely Functional Linux Support Distribution” Eelco Dolstra 2008 Alternatives I started using it 2014 Use cases Nix Support Nix, programming language Nix, package manager History ● Linux ● macOS ● WSL & WSL2 Support NixOS, Linux operating system Alternatives NixOps, deployment tool Use cases Nix Alternatives Divergent Manual configuration Shell scripts History apt, yum, pacman Convergent Support Puppet, Chef, Ansible Alternatives Congruent Docker Guix Use cases Divergent State Time Convergent State Time Congruent State Time Nix Use cases ● Cross-compiling ● Virtual machines (Linux, Windows) ● Automated GUI/service tests History ● chroot environments ● AppImage packages ● RPM and Deb packages Support ● Continuous integration ● Development shells Alternatives ● Docker images Use cases Package management As functions Package management Package Build inputs Dependencies Sources Packages Configuration Build outputs Expressions Artifacts Derivations Build definitions Steps to create artifact from inputs Outputs ● Dependencies ● Build steps ● Artifact Package B Package A Package C Package D Package management Expressions Nix programming language only has expressions Build inputs are values Packages Use functions to define the steps Expressions Derivations Outputs Package management mkDerivation { Packages name = "hello-file"; buildCommand = " Expressions echo 'Hello world' > $out "; Derivations } Outputs Package management mkDerivation { name = "hello-file"; Packages buildInputs = [ hello ]; buildCommand = " Expressions hello > $out "; Derivations } Outputs Package management Packages runCommand "hello-file" { buildInputs = [ hello ]; Expressions } "hello > $out" Derivations Outputs Package management Derivations A tree representing the build to run No more functions, only serialised values Packages nix-instantiate Expressions Derivations Outputs Package management Packages $ nix-instantiate hello-file.nix Expressions /nix/store/sn8i-hello-file.drv Derivations Outputs Package management "outputs": { "out": { "path": "/nix/store/jr4s-hello-file" Packages } } Expressions "inputSrcs": [ Derivations "/nix/store/9krl-default-builder.sh" ] Outputs Package management "inputDrvs": { "/nix/store/5gpb-stdenv-linux.drv": [ "out" ], Packages "/nix/store/ccna-bash-4.4-p23.drv": [ "out" Expressions ], "/nix/store/sddw-hello-2.10.drv": [ Derivations "out" ] Outputs } Package management "platform": "x86_64-linux" "builder": Packages "/nix/store/b9p7-bash-4.4-p23/bin/bash" Expressions "args": [ "-e", Derivations "/nix/store/9krl-default-builder.sh" ] Outputs Package "env": { management "buildCommand": "hello > $out", "buildInputs": "/nix/store/gdh8-hello-2.10", "builder": Packages "/nix/store/b9p7-bash-4.4-p23/bin/bash", "name": "hello-file", Expressions "out": "/nix/store/jr4s-hello-file", "outputs": "out", Derivations "passAsFile": "buildCommand", "stdenv": "/nix/store/r23m-stdenv-linux", "system": "x86_64-linux" Outputs } Package management Outputs Files stored at hash of the derivation Purity is same derivation should give same output Packages Can be checked for purity by building many times Expressions Or we can use a chroot Derivations Caching builds becomes trivial nix-store --realise Outputs Package management Packages $ nix-store --realise /nix/store/sn8i-hello-file.drv /nix/store/jr4s-hello-file Expressions $ cat /nix/store/jr4s-hello-file Hello, world! Derivations Outputs Operating systems As functions Operating systems { services.openssh.enable = true; virtualisation.docker.enable = true; Configuration users.extraUsers.brian = { Generations uid = 1000; extraGroups = [ "wheel" "docker" ]; }; } Operation systems Generations If we change configuration we build a new profile A profile contains kernel, systemd configuration, Configuration /etc files and a directory to be placed on $PATH Profiles are registered as generations Generations Old generations can be collected Generations are shown at boot Image; MichaelLindman (level1techs.com) Development shells Using functions Development shells $ nix-shell -p python $$ which python /nix/store/8818-python-2.7.11/bin/python Usage $ cd myproject Configuration $ nix-shell ## install direnv $ cd myproject Development mkDerivation { shells name = "amkt-env"; buildInputs = [ nodejs-8_x Usage yarn jdk Configuration which curl git flow ] ++ optionals isLinux [ firefox ]; } Docker as functions No Dockerfiles Docker as functions FROM alpine Dockerfile RUN apk add --update cowsay Nix CMD ["/bin/cowsay", "hello"] EXPOSE 8080 Docker as functions Dockerfile What’s the version of Cowsay? What’s the version of Perl? Dockerfile What’s the version of libc? Nix What’s the version of Alpine? Docker as functions dockerTools.buildImage { name = "cowsay-hello"; contents = [ cowsay ]; fromImage = alpine; Dockerfile config = { Cmd = [ "/bin/cowsay" "hello" ]; Nix ExposedPorts = { "8080/tcp" = {}; }; }; } Functional infra One tool for CI, dev and prod. Known versions of everything. All reproducible. Thank you! Functions Simplicity Same inputs always give same output Can always inline and extract expressions Simplicity Evaluation is substitution Reuse Functions Reuse If we see same code, we can always extract it out Using code is just using functions Simplicity Reuse Agenda item 1 Agenda item 2 Agenda Agenda item 3 Agenda item 4 Agenda item 5 Agenda item 6 Highlight sections As you introduce them Agenda Hold *shift* and drag the bar Use *Distribute* tools Found under *Arrange* In the menu above Talk Title Subtitle Talk Title Subtitle Alternate agenda option Alternate agenda option Chapter Titles Chapter subtitles should be two lines or less super awesome things This is a big statement slide. Use dark background for bolder points. This is a big statement slide. Use dark background for bolder points. This is a big statement slide. Use light background for lighter points. This is a big statement slide. Use dark background for bolder points. This is a reference point slide. Likely will be from a site, book, talk, etc. Source Book, Source Author, Other Relevant Info This is a reference point slide. Likely will be from a site, book, talk, etc. Source Book, Source Author, Other Relevant Info This is a quote slide. Keep it tweetable. AUTHOR If you can’t explain it simply, you don’t understand it well enough. ALBERT EINSTEIN Use when Can be used explaining as pro/con, two sides of hypothesis/ a point finding, etc. List out List out The qualities The qualities of the two of the two Different sides Different sides Use another Use another Slide type Slide type If you want to point out If you want to point out The similarities The similarities Talking point Big statement Supporting info for talking point. with support Use no more than two lines. Talking point Manage audience focus by placing bar beneath talking point. Talking point Stay consistent with using the light or dark throughout the related big statement. Spacing Big statement Hold down *Shift* to drag when moving the bar to make sure it with support stays properly aligned. Please remember Using a lot of text on slides is like reading your notes to your audience. Not presenting. Less is more If you don’t need any of the points, don’t use them. We don’t believe in filler, baby! Deeper info slide title Talking point This is arguably the most text that should ever be on one of your presentation slides. Bold text Are they here to read or be engaged? Normal text Don’t ask your audience to read/listen to your notes and call it a presentation. Can send them a document. Move marker Don’t need a third point? Change text style Go ahead and delete! Code sample "contentBylineItems": [ with headers { "context": "addon", "target": { "type": "inlinedialog" }, "tooltip": { "value": "Approvals" }, Bold text "icon": { "url": "/images/approval.png" }, Normal text "name": { "value": "Page Approvals" }, "key": "byline-item", Move marker "url": "/approvals?contentId={content.id}" Change text style } ] Use illustrations from the Graphic Assets Deck to support point. Use illustrations from the Graphic Assets Deck to support point. Keep your big points short & sweet Talking point Supporting text should be brief and no longer than 4 lines. Limit one talking point and image per slide. Talking point Illustrations Separators Utilize spot illustrations Use minimal grey bar to from our design site. separate points/illustrations. Talking point Usage Remember Don’t Obsess These slides have proven Keep the text light and focus The illustrations need only to be versatile in use. on presenting your points. to hint at your point. Talking point Headline 3 Rehearse Focus No worries Subtext type size is Rely on rehearsing Use 4 points It’s OK if the subtext optimized at 14 pt. your voiceover maximum to keep lengths differ. rather than writing. focus. Before After Use to discuss points about how Celebrate how fantastic things are now something worked before. that you’ve implemented changes. Short title, 2 lines max Talking point Supporting text should be brief and no longer than 4 lines. Limit one talking point and image per slide. Point 1 Point 2 Point 3 Point 4 Illustrations > text Use illustrations to represent ideas rather than always relying on