From abd11f65b9c851e6ae2ee3f71a88f8e8f93f7ec3 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Wed, 29 Nov 2023 11:25:24 +0100 Subject: [PATCH] Cleaning --- nushell/config.nu | 4 +-- nushell/config/mount.nu | 65 ++++++++++++++++++++++++++++++++++++++++ nushell/config/tfetch.nu | 8 +++-- nushell/env.nu | 16 +++++----- 4 files changed, 81 insertions(+), 12 deletions(-) create mode 100644 nushell/config/mount.nu diff --git a/nushell/config.nu b/nushell/config.nu index aca39ae..77877c5 100644 --- a/nushell/config.nu +++ b/nushell/config.nu @@ -1,4 +1,6 @@ source ~/.config/nushell/config/init.nu +source ~/.config/nushell/config/extra.nu +source ~/.config/nushell/config/mount.nu source ~/.config/nushell/config/config.nu source ~/.config/nushell/config/aliases.nu source ~/.config/nushell/config/git.nu @@ -8,7 +10,5 @@ source ~/.config/nushell/config/gclone.nu source ~/.config/nushell/config/pass.nu source ~/.config/nushell/config/docker.nu source ~/.config/nushell/config/xrandr.nu -source ~/.config/nushell/config/extra.nu -source ~/.config/nushell/config/extra.nu source ~/.config/nushell/config/tfetch.nu diff --git a/nushell/config/mount.nu b/nushell/config/mount.nu new file mode 100644 index 0000000..e9e25fc --- /dev/null +++ b/nushell/config/mount.nu @@ -0,0 +1,65 @@ +# startx checks if disks are mounted +def startx [] { + # I'm used to running startx right after I login, to this first mount my + # disks and then runs startx. + mount-disk + + # Then I run startx + /bin/startx +} + +# mount my encrypted disks +def mount-disk [] { + + # If there is no $env.PRIMARY_DISK_UUID, we don't have to decrypt / mount anything + if not ("PRIMARY_DISK_UUID" in $env) { + return + } + + # In my home on my encrypted partition, I have an empty file in ~/.mounted, + # this way, I can easily check if my encrypted partition is mounted or not. + if ("~/.mounted"| path exists) { + print -en "already mounted\n" + return + } + + mut passphrase = "" + + # On my computer, I have two disks, one NVMe with my OS, and one HDD with + # more space. I want three attempts at typing the passphrase. + for retry in [1, 2, 3] { + + # Because they're encrypted with the same passphrase, I read the + # passphrase once, and then decrypt both disks. + sudo echo -n + let local_passphrase = (input -s "Enter your passphrase: ") + print -en "\n" + + # Mount the NVMe encrypted partition + let mounted = do { $local_passphrase | sudo cryptsetup luksOpen ("/dev/disk/by-uuid/" + $env.PRIMARY_DISK_UUID) luks } | complete + + # If mounted correctly, break this loop and mount other partitions. + if $mounted.exit_code == 0 { + print -en (ansi green_bold) "Passphrase correct\n" (ansi reset) + $passphrase = $local_passphrase + break + } else if $retry == 3 { + # If the third attempt failed, exit + error make {msg: "couldn't decrypt disk"} + } else { + print -en (ansi red_bold) "Passphrase incorrect, please try again\n" (ansi reset) + } + } + + sudo mount /dev/mapper/luks /home + + if ("SECONDARY_DISK_UUID" in $env) { + $passphrase | sudo cryptsetup luksOpen ("/dev/disk/by-uuid/" + $env.SECONDARY_DISK_UUID) luks2 + sudo mount /dev/mapper/luks2 /mnt + } + + # Some services cannot be started on boot because my encrypted drives are + # not mounted yet, so I mount them here. + systemctl start --user pueued + sudo systemctl start docker +} diff --git a/nushell/config/tfetch.nu b/nushell/config/tfetch.nu index 45fd285..3a578e6 100644 --- a/nushell/config/tfetch.nu +++ b/nushell/config/tfetch.nu @@ -1,7 +1,9 @@ # nushell puts prompt at bottom of terminal on resize, so it's preferable to start on the bottom of the terminal +let has_tfetch = not (which tfetch | is-empty) + # tfetch takes 19 lines, 21 with prompt -let height = (stty size | split row " " | get 0 | into int) - 21 +let height = (stty size | split row " " | get 0 | into int) - (if $has_tfetch { 21 } else { 0 }) # skip lines so that the prompt starts at the bottom of the terminal for i in 0..<$height { @@ -9,4 +11,6 @@ for i in 0..<$height { } # Start tfetch -tfetch +if $has_tfetch { + tfetch +} diff --git a/nushell/env.nu b/nushell/env.nu index 71e04ae..e0516db 100644 --- a/nushell/env.nu +++ b/nushell/env.nu @@ -234,20 +234,20 @@ $env.EDITOR = nvim $env.RUST_BACKTRACE = full # Gclone env -$env.GCLONE_PATH = '/home/thomas/git' +$env.GCLONE_PATH = $env.HOME + '/git' $env.GCLONE_FORCE_SSH = true # Python virtual env -$env.VIRTUAL_ENV = '/home/thomas/.venv' +$env.VIRTUAL_ENV = $env.HOME + '/.venv' # To add entries to PATH (on Windows you might use Path), you can use the following pattern: # $env.PATH = ($env.PATH | split row (char esep) | prepend '/some/path') $env.PATH = ($env.PATH | split row (char esep) - | prepend '/home/thomas/.config/polymny/bin' - | prepend '/home/thomas/.config/dotfiles/bin' - | prepend '/home/thomas/.config/dotfiles/bin-extra' - | prepend '/home/thomas/.cargo/bin' - | prepend '/home/thomas/.npmbin/bin' - | prepend '/home/thomas/.venv/bin' + | prepend ($env.HOME + '/.config/polymny/bin') + | prepend ($env.HOME + '/.config/dotfiles/bin') + | prepend ($env.HOME + '/.config/dotfiles/bin-extra') + | prepend ($env.HOME + '/.cargo/bin') + | prepend ($env.HOME + '/.npmbin/bin') + | prepend ($env.HOME + '/.venv/bin') )