<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hayden Young</title>
    <description>The latest articles on DEV Community by Hayden Young (@hbjy).</description>
    <link>https://dev.to/hbjy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F214847%2F05f8b8ad-892f-4fab-8727-7c8b1a514a36.png</url>
      <title>DEV Community: Hayden Young</title>
      <link>https://dev.to/hbjy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hbjy"/>
    <language>en</language>
    <item>
      <title>Nix, the shell bootstrapper</title>
      <dc:creator>Hayden Young</dc:creator>
      <pubDate>Sun, 19 Jul 2020 16:58:21 +0000</pubDate>
      <link>https://dev.to/hbjy/nix-the-shell-bootstrapper-1j2a</link>
      <guid>https://dev.to/hbjy/nix-the-shell-bootstrapper-1j2a</guid>
      <description>&lt;p&gt;So recently I swapped out Arch Linux for NixOS, mainly because &lt;a href="https://github.com/itshaydendev/starnix"&gt;I could bootstrap my entire PC with a configuration file&lt;/a&gt;. I never really looked into nix as a tool until today. Of course, I've been using nix as a package manager for about a month due to NixOS, but I'd never looked into nix itself for development purposes. And oh boy, was I glad that I did. And here's why.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nix"&gt;&lt;code&gt;&lt;span class="kn"&gt;with&lt;/span&gt; &lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;nixpkgs&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;

&lt;span class="nv"&gt;stdenv&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;mkDerivation&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nv"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"node"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nv"&gt;buildInputs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nv"&gt;nodejs&lt;/span&gt;
  &lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="nv"&gt;shellHook&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;''&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="s2"&gt;    export PATH="$PWD/node_modules/.bin/:$PATH"&lt;/span&gt;&lt;span class="err"&gt;

&lt;/span&gt;&lt;span class="s2"&gt;    alias run='npm run'&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="s2"&gt;  ''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So what the fork is this?! Well, calm down a little and I'll tell you.&lt;/p&gt;

&lt;p&gt;This beautiful piece of functional code you see before you is a Nix Derivation. It's used in this instance to make a complete dev environment for me for nodejs. It lives in a file called &lt;code&gt;default.nix&lt;/code&gt;, and allows for &lt;em&gt;anyone&lt;/em&gt; to just come along and run &lt;code&gt;nix-shell&lt;/code&gt; and have a full Node.js development environment complete with an alias of &lt;code&gt;npm run&lt;/code&gt; to &lt;code&gt;run&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The fact that I can do this without having to go download NVM or nodenv or similar is... Well, let's just be calm and say it saves me a lot of headaches.&lt;/p&gt;

&lt;p&gt;So let's break that file down.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nix"&gt;&lt;code&gt;&lt;span class="kn"&gt;with&lt;/span&gt; &lt;span class="kr"&gt;import&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nv"&gt;nixpkgs&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells Nix to import the &lt;a href="https://github.com/nixos/nixpkgs"&gt;&lt;code&gt;nixpkgs&lt;/code&gt;&lt;/a&gt; package registry, which is the official source for nix packages. This becomes important later.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nix"&gt;&lt;code&gt;&lt;span class="nv"&gt;stdenv&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;mkDerivation&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c"&gt;# ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Does what it says on the tin really. Makes a new nix derivation, defined &amp;amp; configured by the contents of the block. This is what Nix evaluates to produce our dev environment.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nix"&gt;&lt;code&gt;&lt;span class="nv"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"node"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This just gives our derivation a name, duh. I've called it &lt;code&gt;node&lt;/code&gt; here, but you could call it &lt;code&gt;stopwritingpostsondevyoudontknowanything&lt;/code&gt; for all I care.&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nix"&gt;&lt;code&gt;&lt;span class="nv"&gt;buildInputs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="nv"&gt;nodejs&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is where that &lt;code&gt;nixpkgs&lt;/code&gt; import line comes in handy. This is telling Nix to install the latest LTS version (at least the current LTS version recorded in the nixpkgs repo) of Node.js, and make it available to us in the dev environment. You could add anything here. Need to build native extensions? Throw &lt;code&gt;gcc&lt;/code&gt; and &lt;code&gt;automake&lt;/code&gt; in here!&lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nix"&gt;&lt;code&gt;&lt;span class="nv"&gt;shellHook&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;''&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="s2"&gt;  export PATH="$PWD/node_modules/.bin/:$PATH"&lt;/span&gt;&lt;span class="err"&gt;

&lt;/span&gt;&lt;span class="s2"&gt;  alias run='npm run'&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="s2"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So this is essentially a bash script that will be called when the environment is created. In this instance, it's adding the &lt;code&gt;node_modules/.bin&lt;/code&gt; directory to our &lt;code&gt;PATH&lt;/code&gt; variable, so we can call any NPM-installed binaries as if they were globally installed on our system. Really cool for those obscure linting commands you only ever need to run once or twice.&lt;/p&gt;

&lt;p&gt;It also aliases &lt;code&gt;npm run&lt;/code&gt; to &lt;code&gt;run&lt;/code&gt;, allowing me to do &lt;code&gt;run build&lt;/code&gt; instead of &lt;code&gt;npm run build&lt;/code&gt;. Tiny amount of time saved, but I love tiny, insignificant time saving measures.&lt;/p&gt;




&lt;p&gt;So that's the &lt;code&gt;default.nix&lt;/code&gt; file covered.&lt;/p&gt;

&lt;p&gt;But Hayden, you keep telling me how cool and epic and amazing and &amp;lt;insert other synonym for 'cool' here&amp;gt;, but you haven't shown me anything yet.&lt;/p&gt;

&lt;p&gt;no talk me, i angy.&lt;/p&gt;

&lt;p&gt;Well, don't be so angy. You want a demo putting it all together? Have one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://asciinema.org/a/T4Z8hQ9t4NZCohSjdTHJgmFjg"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W3o_4I6D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://asciinema.org/a/T4Z8hQ9t4NZCohSjdTHJgmFjg.png" alt="asciinema" width="880" height="514"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;See? It's fantastic.&lt;/p&gt;

&lt;p&gt;Well, that about wraps this up.&lt;/p&gt;

&lt;p&gt;TL;DR, Nix is brilliant and you should try it out now. Go. Leave the room please. Go. I don't want you around anymore. Go try Nix. Now. BYE!&lt;/p&gt;

</description>
      <category>nix</category>
      <category>node</category>
      <category>todayilearned</category>
      <category>devops</category>
    </item>
    <item>
      <title>Systemd for service management</title>
      <dc:creator>Hayden Young</dc:creator>
      <pubDate>Tue, 02 Jun 2020 11:55:24 +0000</pubDate>
      <link>https://dev.to/hbjy/systemd-for-service-management-67g</link>
      <guid>https://dev.to/hbjy/systemd-for-service-management-67g</guid>
      <description>&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/PC0nwmppb7U"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  LinuxCast 01 - Managing services with systemd
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;systemd&lt;/code&gt; is possibly the most common init system you'll come across on Linux,&lt;br&gt;
it's pre-installed on Ubuntu, Arch, Fedora and many more popular distributions.&lt;/p&gt;

&lt;p&gt;It's also incredibly trivial to manage your services with it, and I'm going to&lt;br&gt;
show you how to do just that in this inaugural LinuxCast.&lt;/p&gt;

&lt;p&gt;So without further ado, let's jump in.&lt;/p&gt;



&lt;p&gt;Starting and stopping services is probably the most common task you'll perform&lt;br&gt;
using systemd. And it's incredibly easy.&lt;/p&gt;

&lt;p&gt;On this computer, I'm running Arch Linux, and I have the containerization tool&lt;br&gt;
'Docker' installed. It's a wonderful tool, but it doesn't automatically start&lt;br&gt;
up. We can start it ourselves very easily and have it run in the background&lt;br&gt;
using the power of systemd.&lt;/p&gt;

&lt;p&gt;The command you use to do any task with systemd is &lt;code&gt;systemctl&lt;/code&gt;, standing for&lt;br&gt;
"system control", &lt;a href="https://bbs.archlinux.org/viewtopic.php?pid=1584556#p1584556"&gt;not cuttlefish&lt;/a&gt;. Who could've seen that coming?&lt;/p&gt;

&lt;p&gt;In this instance, we want to &lt;em&gt;start&lt;/em&gt; a service, so we're going to pop open a&lt;br&gt;
terminal, and we'll type the following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Start up the Docker service&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl start docker.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if all has gone well, you'll notice that there wasn't any output. That's&lt;br&gt;
good. So if we now run &lt;code&gt;docker ps&lt;/code&gt;, we should see that familiar table from&lt;br&gt;
Docker, just as expected.&lt;/p&gt;



&lt;p&gt;But what if we realise it's taking up a ton of RAM, so we want to &lt;em&gt;stop&lt;/em&gt; the&lt;br&gt;
Docker service? For that, there's a very similar subcommand -- &lt;code&gt;stop&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Stop the Docker service&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl stop docker.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now, again, notice how &lt;code&gt;systemctl&lt;/code&gt; gave us no output to work with. Again,&lt;br&gt;
that's what we were expecting. It means that Docker has now stopped. So if we&lt;br&gt;
run &lt;code&gt;docker ps&lt;/code&gt; again, we should find that the client fails to connect to the&lt;br&gt;
Docker socket.&lt;/p&gt;



&lt;p&gt;What if you forgot whether or not you started a service or not? That's easy&lt;br&gt;
too.&lt;/p&gt;

&lt;p&gt;To do that, we want to view the &lt;code&gt;status&lt;/code&gt; of the service. Can you guess the&lt;br&gt;
command for that yet?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Check the status of the Docker service&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl status docker.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Look at that, we have a lot of command-line output. The only thing we're&lt;br&gt;
interested in, however, is the line that says "Active", and we can see that the&lt;br&gt;
Docker service is "inactive" or "dead", meaning the service isn't running right&lt;br&gt;
now.&lt;/p&gt;



&lt;p&gt;Now, what about starting services when your computer boots up? Well, to&lt;br&gt;
control whether services do that or not, we can use the &lt;code&gt;enable&lt;/code&gt; and &lt;code&gt;disable&lt;/code&gt;&lt;br&gt;
&lt;code&gt;systemctl&lt;/code&gt; commands.&lt;/p&gt;

&lt;p&gt;Let's say I want Docker to start when my computer does. That's easily done.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Tell systemd to start Docker on boot&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl &lt;span class="nb"&gt;enable &lt;/span&gt;docker.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This time round, we get output. Systemd is telling us that it's created&lt;br&gt;
a symlink from the service file for Docker into the directory containing all&lt;br&gt;
the services that are run on startup.&lt;/p&gt;

&lt;p&gt;What about if I want to stop Docker starting when my computer does? Again,&lt;br&gt;
easily done. It's as easy as using that &lt;code&gt;disable&lt;/code&gt; command I mentioned earlier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Tell systemd to stop running Docker on boot&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl disable docker.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we can see it's removed the docker service from that folder from earlier.&lt;/p&gt;

&lt;p&gt;Now Docker will no longer start when our computer does.&lt;/p&gt;




&lt;p&gt;Thanks for giving this a read. Drop me a follow if you found it useful, I'll be writing some more of these.&lt;/p&gt;

</description>
      <category>linux</category>
      <category>systemd</category>
      <category>sysadmin</category>
    </item>
  </channel>
</rss>
