0.55 issue with require

Hyprland version
`0.55`

Describe your issue / feature…

Hello

I migrated my config to lua, and everything works fine, but there is one strange issue. My bindings and my programs give an error message when they’re separate and require’d, but not when they’re part of the hyprland.lua file. Programs.lua gives a (string expected, got nil) error, bindings.lua gives ‘unexpected symbol near </194>

What’s happening here? Apparently the same code is fine when it is part of hyprland.lua

Just guessing here.

If the lua file you need to require is named input.lua, in your main file you would use,

require(“input”)

If you have input.lua in a subfolder named configs (I.e., configs/input.lua),

require(“configs.input”)

No, that’s not it, because both program.lua and bindings.lua are in the same directory as hyprland.lua

I was guessing because I can’t see your code. You would get a ‘string expected, got nil’ error if your code was require(programs) instead of require(“programs”).

You would see an unexpected error message if you were to use something like, require(%) .

Outside of those guesses, I’d have to see your code.

Here is the code:

---------------------
---- KEYBINDINGS ----
---------------------

local mainMod = "SUPER" -- Sets "Windows" key as main modifier

-- Example binds, see https://wiki.hypr.land/Configuring/Basics/Binds/ for more
hl.bind(mainMod .. " + RETURN", hl.dsp.exec_cmd(terminal))

local closeWindowBind = hl.bind(mainMod .. " + Q", hl.dsp.window.close())
-- closeWindowBind:set_enabled(false)

hl.bind(mainMod .. " + E", hl.dsp.exec_cmd("command -v hyprshutdown >/dev/null 2>&1 && hyprshutdown || hyprctl dispatch 'hl.dsp.exit()'"))
hl.bind(mainMod .. " + T", hl.dsp.exec_cmd(fileManager))
-- hl.bind(mainMod .. " + V", hl.dsp.window.float({ action = "toggle" }))
hl.bind(mainMod .. " + R", hl.dsp.exec_cmd(menu))
-- hl.bind(mainMod .. " + P", hl.dsp.window.pseudo())
-- hl.bind(mainMod .. " + J", hl.dsp.layout("togglesplit"))    -- dwindle only

hl.bind(mainMod .. " + M", hl.dsp.window.fullscreen({ mode = "maximized" , action = "toggle" }))
hl.bind(mainMod .. " + F", hl.dsp.window.fullscreen({ mode = "fullscreen" , action = "toggle" }))

-- Move focus with mainMod + arrow keys
hl.bind(mainMod .. " + left",  hl.dsp.focus({ direction = "left" }))
hl.bind(mainMod .. " + right", hl.dsp.focus({ direction = "right" }))
hl.bind(mainMod .. " + up",    hl.dsp.focus({ direction = "up" }))
hl.bind(mainMod .. " + down",  hl.dsp.focus({ direction = "down" }))

-- Switch workspaces with mainMod + [0-9]
-- Move active window to a workspace with mainMod + SHIFT + [0-9]
for i = 1, 10 do
    local key = i % 10 -- 10 maps to key 0
    hl.bind(mainMod .. " + " .. key,             hl.dsp.focus({ workspace = i}))
    hl.bind(mainMod .. " + SHIFT + " .. key,     hl.dsp.window.move({ workspace = i }))
end

-- Example special workspace (scratchpad)
hl.bind(mainMod .. " + S",         hl.dsp.workspace.toggle_special("magic"))
hl.bind(mainMod .. " + SHIFT + S", hl.dsp.window.move({ workspace = "special:magic" }))

-- Scroll through existing workspaces with mainMod + scroll
hl.bind(mainMod .. " + mouse_down", hl.dsp.focus({ workspace = "e+1" }))
hl.bind(mainMod .. " + mouse_up",   hl.dsp.focus({ workspace = "e-1" }))

-- Move/resize windows with mainMod + LMB/RMB and dragging
hl.bind(mainMod .. " + mouse:272", hl.dsp.window.drag(),   { mouse = true })
hl.bind(mainMod .. " + mouse:273", hl.dsp.window.resize(), { mouse = true })

-- Laptop multimedia keys for volume and LCD brightness
hl.bind("XF86AudioRaiseVolume", hl.dsp.exec_cmd("wpctl set-volume -l 1 @DEFAULT_AUDIO_SINK@ 5%+"), { locked = true, repeating = true })
hl.bind("XF86AudioLowerVolume", hl.dsp.exec_cmd("wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%-"),      { locked = true, repeating = true })
hl.bind("XF86AudioMute",        hl.dsp.exec_cmd("wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle"),     { locked = true, repeating = true })
hl.bind("XF86AudioMicMute",     hl.dsp.exec_cmd("wpctl set-mute @DEFAULT_AUDIO_SOURCE@ toggle"),   { locked = true, repeating = true })
hl.bind("XF86MonBrightnessUp",  hl.dsp.exec_cmd("brightnessctl -e4 -n2 set 5%+"),                  { locked = true, repeating = true })
hl.bind("XF86MonBrightnessDown",hl.dsp.exec_cmd("brightnessctl -e4 -n2 set 5%-"),                  { locked = true, repeating = true })

-- Requires playerctl
hl.bind("XF86AudioNext",  hl.dsp.exec_cmd("playerctl next"),       { locked = true })
hl.bind("XF86AudioPause", hl.dsp.exec_cmd("playerctl play-pause"), { locked = true })
hl.bind("XF86AudioPlay",  hl.dsp.exec_cmd("playerctl play-pause"), { locked = true })
hl.bind("XF86AudioPrev",  hl.dsp.exec_cmd("playerctl previous"),   { locked = true })
```

---------------------
---- MY PROGRAMS ----
---------------------

-- Set programs that you use
local terminal    = "uwsm app -- alacritty"
local fileManager = "uwsm app -- thunar"
local menu        = "uwsm app -- $(tofi-drun)"



Ah. Your programs.lua (“MY PROGRAMS”) file contains local variables that you are trying to reference in another module.

If these variables are used elsewhere, remove the “local” declaration.

If the variables are only used in your bindings.lua file, move those local assignments to the top of bindings.lua .

Thanks. I moved them to bindings.lua and require’d that in hyperland.lua. That worked. Making them global in programs.lua didn’t work for some reason.

Thanks for your help. I’ll study the lua documentation some more.

Based on the config you posted, I think if you wanted them in programs.lua you should probably make it a module, and return it. The code you put above doesn’t return anything, which would mean you have to make all vars global.
So something like this:

---------------------
---- MY PROGRAMS ----
---------------------
local M = {}

-- Set programs that you use
M.terminal    = "uwsm app -- alacritty"
M.fileManager = "uwsm app -- thunar"
M.menu        = "uwsm app -- $(tofi-drun)"

return M

Then you could use it like:

---------------------
---- KEYBINDINGS ----
---------------------
local progs = require("programs")
local mainMod = "SUPER" -- Sets "Windows" key as main modifier

-- Example binds, see https://wiki.hypr.land/Configuring/Basics/Binds/ for more
hl.bind(mainMod .. " + RETURN", hl.dsp.exec_cmd(progs.terminal))

OR

---------------------
---- KEYBINDINGS ----
---------------------
local term = require("programs").terminal
local mainMod = "SUPER" -- Sets "Windows" key as main modifier

-- Example binds, see https://wiki.hypr.land/Configuring/Basics/Binds/ for more
hl.bind(mainMod .. " + RETURN", hl.dsp.exec_cmd(term))

OR

---------------------
---- KEYBINDINGS ----
---------------------
local mainMod = "SUPER" -- Sets "Windows" key as main modifier

-- Example binds, see https://wiki.hypr.land/Configuring/Basics/Binds/ for more
hl.bind(mainMod .. " + RETURN", hl.dsp.exec_cmd(require("programs").terminal)