2022-01-09 01:20:46 +01:00
{ depot , pkgs , lib , . . . }:
let
bins = depot . nix . getBins pkgs . stow [ " s t o w " ]
2022-01-30 17:06:58 +01:00
// depot . nix . getBins pkgs . coreutils [ " m k d i r " " l n " " p r i n t e n v " " r m " ]
// depot . nix . getBins pkgs . xe [ " x e " ]
// depot . nix . getBins pkgs . lr [ " l r " ]
// depot . nix . getBins pkgs . nix [ " n i x - s t o r e " ]
;
2022-01-09 01:20:46 +01:00
# run stow to populate the target directory with the given stow package, read from stowDir.
# Bear in mind that `stowDirOriginPath` should always be semantically bound to the given `stowDir`, otherwise stow might become rather confused.
2022-01-30 17:06:58 +01:00
runStow =
{
# “stow package” to stow (see manpage)
stowPackage
, # “target directory” to stow in (see manpage)
targetDir
, # The “stow directory” (see manpage), containing “stow packages” (see manpage)
stowDir
, # representative directory for the stowDir in the file system, against which stow will create relative links.
# ATTN: this is always overwritten with the contents of `stowDir`! You shouldn’ t re-use the same `stowDirOriginPath` for different `stowDir`s, otherwise there might be surprises.
stowDirOriginPath
,
} : depot . nix . writeExecline " s t o w - ${ stowPackage } " { } [
# first, create a temporary stow directory to use as source
# (stow will use it to determine the origin of files)
" i f "
[ bins . mkdir " - p " stowDirOriginPath ]
# remove old symlinks
" i f "
[
" p i p e l i n e "
[
bins . lr
" - 0 "
" - t "
" d e p t h = = 1 & & t y p e = = l "
stowDirOriginPath
]
bins . xe
" - 0 "
bins . rm
2022-01-09 01:20:46 +01:00
]
2022-01-30 17:06:58 +01:00
# create an indirect gc root so our config is not cleaned under our asses by a garbage collect
" i f "
[
bins . nix-store
2022-01-09 10:24:22 +01:00
" - - r e a l i s e "
" - - i n d i r e c t "
2022-01-30 17:06:58 +01:00
" - - a d d - r o o t "
" ${ stowDirOriginPath } / . n i x - s t o w d i r - g c - r o o t "
2022-01-09 10:24:22 +01:00
stowDir
2022-01-30 17:06:58 +01:00
]
# populate with new stow targets
" i f "
[
" e l g l o b "
" - w 0 "
" s t o w P a c k a g e s "
" ${ stowDir } / * "
bins . ln
" - - f o r c e "
" - s t "
stowDirOriginPath
" $ s t o w P a c k a g e s "
]
# stow always looks for $HOME/.stowrc to read more arguments
" e x p o r t "
" H O M E "
" / h o m e l e s s - s h e l t e r "
bins . stow
2022-01-09 01:20:46 +01:00
# always run restow for now; this does more stat but will remove stale links
" - - r e s t o w "
2022-01-30 17:06:58 +01:00
" - - d i r "
stowDirOriginPath
" - - t a r g e t "
targetDir
2022-01-09 01:20:46 +01:00
stowPackage
2022-01-30 17:06:58 +01:00
] ;
2022-01-09 01:20:46 +01:00
# create a stow dir from a list of drv paths and a stow package name.
makeStowDir =
( with depot . nix . yants ;
2022-01-30 17:06:58 +01:00
defun
[
( list ( struct {
2022-01-09 01:20:46 +01:00
originalDir = drv ;
stowPackage = string ;
} ) )
drv
2022-01-30 17:06:58 +01:00
] )
( dirs :
depot . nix . runExecline " m a k e - s t o w - d i r "
{
stdin = lib . pipe dirs [
( map depot . users . Profpatsch . netencode . gen . dwim )
depot . users . Profpatsch . netstring . toNetstringList
] ;
} [
" i m p o r t a s "
" o u t "
" o u t "
" i f "
[ bins . mkdir " - p " " $ o u t " ]
" f o r s t d i n "
" - d "
" "
" - o "
" 0 "
" l i n e "
" p i p e l i n e "
[
depot . users . Profpatsch . execline . print-one-env
" l i n e "
]
depot . users . Profpatsch . netencode . record-splice-env
" i m p o r t a s "
" - u i "
" o r i g i n a l D i r "
" o r i g i n a l D i r "
" i m p o r t a s "
" - u i "
" s t o w P a c k a g e "
" s t o w P a c k a g e "
bins . ln
" - s T "
" $ o r i g i n a l D i r "
" \$ { o u t } / \$ { s t o w P a c k a g e } "
] ) ;
2022-01-09 01:20:46 +01:00
in
2022-01-09 14:43:07 +01:00
# TODO: temp setup
2022-01-30 17:06:58 +01:00
lib . pipe { } [
2022-01-09 01:20:46 +01:00
( _ : makeStowDir [ {
2022-01-09 14:43:07 +01:00
stowPackage = " s c r i p t s " ;
originalDir = pkgs . linkFarm " s c r i p t s - f a r m " [
2022-01-30 17:06:58 +01:00
{
name = " s c r i p t s / y t e x t r " ;
path = depot . users . Profpatsch . ytextr ;
}
] ;
2022-01-09 01:20:46 +01:00
} ] )
( d : runStow {
stowDir = d ;
2022-01-09 14:43:07 +01:00
stowPackage = " s c r i p t s " ;
targetDir = " / h o m e / p h i l i p " ;
stowDirOriginPath = " / h o m e / p h i l i p / . l o c a l / s h a r e / n i x - h o m e / s t o w - o r i g i n " ;
2022-01-09 01:20:46 +01:00
} )
]