You are not logged in.

#1 2026-10-09 15:20:59

lenhuppe
Member
From: New Hampshire USA
Registered: 2018-12-10
Posts: 318
Website

SOLVED Configuring custom Nushell functions

I am tinkering with Nushell lately and I have a configuration issue. I have a number of custom scripts and functions that I use, and in Fish I am able to add my functions to my scripts. However in Nushell that is not yet the case.

So far I can run my scripts from the command line by storing them in the autoload subdirectory. I can also run my functions from the command line by keeping them in a single file that I source from my config.nu file.

My issue is that I cannot include my functions in my scripts. I have been reading about modules but so far I have been unable to get that approach to work. I am running out of ideas to try.

I understand that Nushell is still in the early stages at version 0.116.1 but there are people using it in production even now. Any Nushell gurus out there?

Here are my functions kept in a module named funcs:

functions.nu

#
# my functions
#

module funcs {

    # print status messages to the screen
    export def prnt [arg: string] {
        $"\n\t(ansi green)($arg)(ansi reset)\n"
    }

    # print informational messages to the screen
    export def mesg [arg: string] {
        $"\n\t(ansi yellow)($arg)(ansi reset)\n"
    }

    # print alert messages to the screen
    export def alrt [arg: string] {
        $"\n\t(ansi red)($arg)(ansi reset)\n"
    }

    # confirm before continuing a script
    export def confirm [] {
        while true {
            let reply = (input "Continue? [y/n]: ")
            if $reply == y {
                break
            } else if $reply == n {
                print $"(ansi red)Aborting(ansi reset)"
                return 1
            } else {
                print $"(ansi red)Please enter y or n(ansi reset)"
                continue
            }
        }
    }
}

Here is my config so far:

config.nu

#
# nu config
#

# functions
source ~/.config/nushell/functions.nu
use funcs * # for interactive mode

# hooks
source ~/.config/nushell/hooks.nu

# abbreviations expose the executed command
# $env.config.abbreviations = {
#    ls: "ls -as"
# }

# aliases hide the executed command
# do not alias: ls
alias blk = lsblk -o name,fstype,label,tran,mountpoints
alias eza = eza --icons -F=always -aaglM@
alias fdupes = fdupes -rS
alias free = sys mem
alias g-d = gnome_desktop.fish
alias gdu = gdu -a
alias graphical = sudo systemctl set-default graphical.target
alias grep = ugrep -izr
alias lock = xdg-screensaver lock
alias lsa = ls -as
alias moon = curl wttr.in/moon
alias multi-user = sudo systemctl set-default multi-user.target
alias nvim = nvim -n
alias opn = nautilus ($env.PWD)
alias pkgfile = pkgfile -i
alias tree = tree -apCF
alias vi = nvim -n
alias vim = nvim -n
alias wttr = curl wttr.in/Manchester+NH

# prompt
use ~/.config/nushell/my-prompt.nu *
$env.PROMPT_INDICATOR = {|| "" }
$env.PROMPT_COMMAND = {|| left-prompt }
$env.PROMPT_COMMAND_RIGHT = {||
    date now | format date "%d-%a %r"
}

# fastfetch
fastfetch -c paleofetch

And my env setup:

env.nu

#
# nu env
#

# table --list
# env.config.table.mode = ''

# path is inheritrd from fish?
# $env.PATH ++= [ "~/.local/bin" ]
# $env.PATH = [ "~/.local/bin" ] ++ $env.PATH

# Add custom directories:
# const NU_LIB_DIRS = [
#     ($nu.default-config-dir | path join 'scripts')
#     ($nu.data-dir | path join 'completions')
# ]

# An environment variable version ($env.NU_LIB_DIRS) is searched after the constant.

# misc
$env.rm_always_trash = false
$env.auto_cd_implicit = true
$env.show_hints = true
$env.use_kitty_protocol = true
$env.config.show_banner = false

# env setup
export-env {
    load-env {
        BROWSER: "firefox"
        PAGER: "less"
        # MAN_PAGER: "bat -plman --strip-ansi=auto"
        HOSTNAME:  (sys host | get hostname) # uname | get nodename
    }
}

# editor
if (sys host | get hostname) == archie {
    export-env {
        load-env {
            BUFFER_EDITOR: "gnome-text-editor"
            SUDO_EDITOR: "gnome-text-editor"
            EDITOR: "gnome-text-editor"
            VISUAL: "gnome-text-editor"
        }
    }
} else if (sys host | get hostname) == lurch {
    export-env {
        load-env {
            BUFFER_EDITOR: "nvim -n"
            SUDO_EDITOR: "nvim -n"
            EDITOR: "nvim -n"
            VISUAL: "nvim -n"
        }
    }
}

# history
$env.config.history = {
    file_format: plaintext
    max_size: 500
    sync_on_enter: true
    isolation: false
}

# fzf
$env.FZF_DEFAULT_OPTS = '
    --color=info:#ffd800
    --color=bg:#111111
    --color=bg+:#222222
    --color=hl:#00ff00
    --color=hl+:#ff00ff
    --color=fg:white
    --color=fg+:cyan
    --color=pointer:#ff00ff
	--color=prompt:#00ff00
	--pointer="➤"
	--marker="◆"
	--prompt="❯ "
	--border="rounded"
	--border-label=""
	--preview-window="border-rounded"
	--separator="─"
	--scrollbar="│"
    '
# --color=spinner:#e6db74
# --color=header:red
# --color=marker:#ffd800
# --color=border:#808080

]

This is one of my functions:

pac.nu

#
# update system with pacman
#
export def pac [] {
    use funcs *
    prnt 'Updating with Pacman'
    sudo systemctl start mirrorlist.service
    echo
    sudo pacman -Syu
}

If I run pac from the command line, it runs ok but the message does not display.

Last edited by lenhuppe (2026-10-10 01:07:15)

Offline

#2 2026-10-09 16:55:21

schard
Forum Moderator
From: Hannover
Registered: 2016-05-06
Posts: 2,720
Website

Re: SOLVED Configuring custom Nushell functions

I dont see pac.nu being imported anywhere.


Inofficial first vice president of the Rust Evangelism Strike Force

Online

#3 2026-10-09 17:19:20

lenhuppe
Member
From: New Hampshire USA
Registered: 2018-12-10
Posts: 318
Website

Re: SOLVED Configuring custom Nushell functions

schard wrote:

I dont see pac.nu being imported anywhere.

I have not figured out how to use a scripts directory even thought it is part of the NU_LIB_DIRS path.
As a workaround I have it in the autoload directory.
I tried sourcing pac.nu from config.nu but did not work

Offline

#4 2026-10-10 01:06:32

lenhuppe
Member
From: New Hampshire USA
Registered: 2018-12-10
Posts: 318
Website

Re: SOLVED Configuring custom Nushell functions

Updated on Oct 12, 2026

I thought that I had a solution but it turned out to be more complicated. From digging around I learned Nushell does not support sourcing the contents of a directory.

Instead you have to create a mod.nu file which defines the modules found in the directory. Then you can add 'use scripts/' to your config.nu file.

I created a hack to get around that but its a hack. Using modules as intended will require a full rewrite of my config.

Last edited by lenhuppe (2026-10-12 22:40:00)

Offline

Board footer

Powered by FluxBB