# Quality of Life Scripts

**URL:** https://discourse.nixos.org/t/quality-of-life-scripts/2673
**Category:** Uncategorized
**Created:** [April 13, 2019, 6:08am UTC](https://discourse.nixos.org/t/quality-of-life-scripts/2673 "2019-04-13T06:08:54Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Growpotkin](https://discourse.nixos.org/user_avatar/discourse.nixos.org/growpotkin/32/343_2.png) [@Growpotkin](https://discourse.nixos.org/u/Growpotkin)
#### Post date: [April 13, 2019, 6:08am UTC](https://discourse.nixos.org/t/quality-of-life-scripts/2673/1 "2019-04-13T06:08:55Z")

</div>

I imagine most of us each have a little treasure trove of little scripts and shell aliases that make our lives with Nix just a little bit sweeter.  
I invite you to share!

I have my larger [fuzzy finder](https://discourse.nixos.org/t/handy-scripts-for-fuzzy-searching-nixpkgs-and-nixos-options/1659/9) script that has it’s own topic (a bit big to repost so I’ll just link it). This also has a Deoplete (vim) source for completing Options and Packages, pulled from Nix’s env so it will complete all of your overlayed modules and packages.

on the simpler side are these small shell functions to hop into the store path of a program/derivation that I use to sniff around sources.

```auto
# `nat fzf` or `nat nixpkgs.fzf` => prints store path of fzf's derivation
function nat () { nix path-info nixpkgs.${1#nixpkgs.} }
function ngo () { cd `nixat ${1}`}

```

I know I have some others floating around that I will add later, and I am sure collectively we have a ton of convenient little helpers. ❤

---

<div class="post-metadata">

### Author: ![t184256](https://discourse.nixos.org/user_avatar/discourse.nixos.org/t184256/32/1038_2.png) [@t184256](https://discourse.nixos.org/u/t184256)
#### Post date: [April 13, 2019, 6:31am UTC](https://discourse.nixos.org/t/quality-of-life-scripts/2673/2 "2019-04-13T06:31:02Z")

</div>

I have a xonsh alias I named ‘w’ that is a convenience wrapper for nix-shell. I’d call it ‘with’, but ‘with’ is a reserved keyword in xonsh. It’s kinda messy at the moment, but I love it

```auto
# add a powerful 'w' ('with') command.
# Examples:
# w ncdu /
# w extra_file.nix ncdu /
# w ncdu -- ncdu /
# w - ncdu /
# w ncdu --- /
# w - sudo ncdu /
# w py:numpy,notebook -- python -m notebook
def _w(args):
    def expand_arg(arg): # what to turn a dependency word into?
        if arg.endswith('.nix'): # a file
            return [arg]
        elif arg.startswith('py:'): # ex: py:numpy,notebook
            return ['-p',
                    'python3.withPackages (ps: with ps; [ ' +
                    ' '.join(arg.split(':', 1)[1].split(',')) +
                    '])']
        elif arg.startswith('xsh:'): # ex: xsh:numpy,notebook
            return ['-p',
                    'xonsh.overridePythonAttrs(old: ' +
                    '{propagatedBuildInputs = old.propagatedBuildInputs ' +
                    '++ (with python3Packages; [ ' +
                    ' '.join(arg.split(':', 1)[1].split(',')) +
                    ']);})']
        else: # probably a package name
            return ['-p', arg]

    cmd = []
    if '--' in args: # run cmd, ex: with ncdu -- ncdu /
        i = args.index('--')
        args, cmd = args[:i], args[i + 1:]
    elif '-' in args: # pull in following argument, ex: with - ncdu /
        i = args.index('-')
        if args[i + 1] != 'sudo':
            args, cmd = args[:i] + [args[i + 1]], args[i + 1:]
        else: # 'forgot sudo' scenario: with file - sudo file -s /dev/sdc*
            args, cmd = args[:i] + [args[i + 2]], ['sudo'] + args[i + 2:]
    elif '---' in args: # run last dependency, ex: with ncdu --- /
        i = args.index('---')
        args, cmd = args[:i], [args[i - 1]] + args[i + 1:]
    dir_tail = $(pwd).strip().split('/')[-1]
    features = ['+' + a for a in args] if args else ['@' + dir_tail]
    with_features = ' '.join(features + ${...}.get('WITH_FEATURES', '').split())
    flatten = lambda l: [item for sublist in l for item in sublist]
    args = flatten([expand_arg(a) for a in args])
    with ${...}.swap(WITH_FEATURES=with_features):
        with ${...}.swap(WITH_FEATURES_IMMEDIATE=' '.join(features)):
             nix-shell @(args) --run @(' '.join(cmd) if cmd else '.xonsh-wrapped')
aliases['w'] = _w
def _with_completer(prefix, line, begidx, endidx, ctx):
    if not line.startswith('w '):
        return set()
    if '-' in line or '--' in line or '---' in line:
        return set()
    import xonsh.completers.bash_completion
    # TODO: completers for py: and xsh:
    l = line.replace('w ', 'nix-shell -p ', 1)
    r = xonsh.completers.bash_completion.bash_complete_line(l)
    r = {x.replace('nix-shell -p ', 'w ', 1).split()[-1] for x in r}
    return r
completer add with _with_completer
del _w

```

What is not mentioned above is that my right prompt has a list of stuff (`WITH_FEATURES`) that I have summoned in my shell, divided into two sublists of different colors, for the innermost shell and for all the others. This way I always see what extra tools I have and I have a good estimate of what I will lose by pressing ^D. I have no idea how to live without that now.

---

<div class="post-metadata">

### Author: ![t184256](https://discourse.nixos.org/user_avatar/discourse.nixos.org/t184256/32/1038_2.png) [@t184256](https://discourse.nixos.org/u/t184256)
#### Post date: [April 13, 2019, 6:47am UTC](https://discourse.nixos.org/t/quality-of-life-scripts/2673/3 "2019-04-13T06:47:37Z")

</div>

While we are at it, some other aliases of mine:

- `nxg` syncs my configuration changes with git (stashing uncommitted changes and restoring them)
- `nxu` updates channels
- `nxo` builds my configuration on a beefy buildhost
- `nxl` builds my configuration locally with a nice progressbar
- `nxt` is `sudo nixos-rebuild test` that doesn’t create a link
- `nxf` is same with `--fast`
- `nxs` is `sudo nixos-rebuild switch`

and all of them have `--cores 4`.

Moreover, I have autogenerated aliases for a metric ton of their sensible combinations, like `nxguos`: sync git, update channels, build offloaded and switch:

```auto
$ ', '.join([a for a in aliases if a.startswith('nx') and len(a) > 3])
'nxlt, nxlf, nxls, nxot, nxof, nxos, nxut, nxuf, nxus, nxult, nxulf, nxuls, nxul, nxuot, nxuof, nxuos, nxuo, nxup, nxgt, nxgf, nxgs, nxglt, nxglf, nxgls, nxgl, nxgot, nxgof, nxgos, nxgo, nxgp, nxgut, nxguf, nxgus, nxgu, nxgult, nxgulf, nxguls, nxgul, nxguot, nxguof, nxguos, nxguo, nxgup'

```

Gist: [nx-aliases · GitHub](https://gist.github.com/t184256/0ca8d6941b45ee7abdf932b03e51cacf)

---

<div class="post-metadata">

### Author: ![lheckemann](https://discourse.nixos.org/user_avatar/discourse.nixos.org/lheckemann/32/415_2.png) [@lheckemann](https://discourse.nixos.org/u/lheckemann)
#### Post date: [April 13, 2019, 8:46am UTC](https://discourse.nixos.org/t/quality-of-life-scripts/2673/4 "2019-04-13T08:46:12Z")

</div>

> [@t184256](#):
>
> and all of them have `--cores 4` .

Any particular reason not to just set this globally with `nix.buildCores`?

---

<div class="post-metadata">

### Author: ![t184256](https://discourse.nixos.org/user_avatar/discourse.nixos.org/t184256/32/1038_2.png) [@t184256](https://discourse.nixos.org/u/t184256)
#### Post date: [April 13, 2019, 9:00am UTC](https://discourse.nixos.org/t/quality-of-life-scripts/2673/5 "2019-04-13T09:00:57Z")

</div>

Not really. Fixed. Thanks!

---

<div class="post-metadata">

### Author: ![Growpotkin](https://discourse.nixos.org/user_avatar/discourse.nixos.org/growpotkin/32/343_2.png) [@Growpotkin](https://discourse.nixos.org/u/Growpotkin)
#### Post date: [April 13, 2019, 5:26pm UTC](https://discourse.nixos.org/t/quality-of-life-scripts/2673/6 "2019-04-13T17:26:31Z")

</div>

Whoa I had never heard of this shell before. Reminds me a bit of Turtle in Haskell; this seems rad.

---

<div class="post-metadata">

### Author: ![Growpotkin](https://discourse.nixos.org/user_avatar/discourse.nixos.org/growpotkin/32/343_2.png) [@Growpotkin](https://discourse.nixos.org/u/Growpotkin)
#### Post date: [April 13, 2019, 5:30pm UTC](https://discourse.nixos.org/t/quality-of-life-scripts/2673/7 "2019-04-13T17:30:46Z")

</div>

I had never thought of auto-generating aliases like this. I have seen this kind of laundry list for classics like Oh-my-Zsh’s `git` aliases; but generating a collection this way is so clean. Thanks for post. I’m going to try and expand my `nixos-rebuild` aliases beyond my little `nrs='nixos-rebuild switch'` haha.

---

<div class="post-metadata">

### Author: ![t184256](https://discourse.nixos.org/user_avatar/discourse.nixos.org/t184256/32/1038_2.png) [@t184256](https://discourse.nixos.org/u/t184256)
#### Post date: [April 13, 2019, 5:39pm UTC](https://discourse.nixos.org/t/quality-of-life-scripts/2673/9 "2019-04-13T17:39:19Z")

</div>

I wouldn’t call that these particular scripts of mine ‘clean’, but yeah, xonsh is rad. I’d recommend it to any pythonista without hesitation. It totally kicks ass if you’re doing data analysis, it is usable as one’s daily shell, and it makes shell scripting so trivial it feels like cheating.
