19 import xml
.dom
.minidom
39 # Use xdg module when it's less painful to have as a dependency
42 class XDG(NamedTuple
):
47 XDG_CACHE_HOME
=os
.getenv(
49 os
.path
.expanduser('~/.cache')))
52 class VerificationError(Exception):
58 def __init__(self
) -> None:
61 def status(self
, s
: str) -> None:
62 print(s
, end
=' ', file=sys
.stderr
, flush
=True)
63 self
.line_length
+= 1 + len(s
) # Unicode??
66 def _color(s
: str, c
: int) -> str:
67 return f
'\033[{c:2d}m{s}\033[00m'
69 def result(self
, r
: bool) -> None:
70 message
, color
= {True: ('OK ', 92), False: ('FAIL', 91)}
[r
]
72 cols
= shutil
.get_terminal_size().columns
or 80
73 pad
= (cols
- (self
.line_length
+ length
)) % cols
74 print(' ' * pad
+ self
._color
(message
, color
), file=sys
.stderr
)
77 raise VerificationError()
79 def check(self
, s
: str, r
: bool) -> None:
87 Digest16
= NewType('Digest16', str)
88 Digest32
= NewType('Digest32', str)
91 class ChannelTableEntry(types
.SimpleNamespace
):
99 class AliasPin(NamedTuple
):
103 class SymlinkPin(NamedTuple
):
105 def release_name(self
) -> str:
109 class GitPin(NamedTuple
):
114 class ChannelPin(NamedTuple
):
121 Pin
= Union
[AliasPin
, SymlinkPin
, GitPin
, ChannelPin
]
124 def copy_to_nix_store(v
: Verification
, filename
: str) -> str:
125 v
.status('Putting tarball in Nix store')
126 process
= subprocess
.run(
127 ['nix-store', '--add', filename
], stdout
=subprocess
.PIPE
)
128 v
.result(process
.returncode
== 0)
129 return process
.stdout
.decode().strip() # type: ignore # (for old mypy)
132 def symlink_archive(v
: Verification
, path
: str) -> str:
133 with tempfile
.TemporaryDirectory() as td
:
134 archive_filename
= os
.path
.join(td
, 'link.tar.gz')
135 os
.symlink(path
, os
.path
.join(td
, 'link'))
136 with tarfile
.open(archive_filename
, mode
='x:gz') as t
:
137 t
.add(os
.path
.join(td
, 'link'), arcname
='link')
138 return copy_to_nix_store(v
, archive_filename
)
141 class AliasSearchPath(NamedTuple
):
144 # pylint: disable=no-self-use
145 def pin(self
, _
: Verification
, __
: Optional
[Pin
]) -> AliasPin
:
149 class SymlinkSearchPath(NamedTuple
):
152 # pylint: disable=no-self-use
153 def pin(self
, _
: Verification
, __
: Optional
[Pin
]) -> SymlinkPin
:
156 def fetch(self
, v
: Verification
, _
: Pin
) -> str:
157 return symlink_archive(v
, self
.path
)
160 class GitSearchPath(NamedTuple
):
164 def pin(self
, v
: Verification
, old_pin
: Optional
[Pin
]) -> GitPin
:
165 _
, new_revision
= git_cache
.fetch(self
.git_repo
, self
.git_ref
)
166 if old_pin
is not None:
167 assert isinstance(old_pin
, GitPin
)
168 verify_git_ancestry(v
, self
, old_pin
.git_revision
, new_revision
)
169 return GitPin(release_name
=git_revision_name(v
, self
, new_revision
),
170 git_revision
=new_revision
)
172 def fetch(self
, v
: Verification
, pin
: Pin
) -> str:
173 assert isinstance(pin
, GitPin
)
174 git_cache
.ensure_rev_available(
175 self
.git_repo
, self
.git_ref
, pin
.git_revision
)
176 return git_get_tarball(v
, self
, pin
)
179 class ChannelSearchPath(NamedTuple
):
184 def pin(self
, v
: Verification
, old_pin
: Optional
[Pin
]) -> ChannelPin
:
185 if old_pin
is not None:
186 assert isinstance(old_pin
, ChannelPin
)
188 channel_html
, forwarded_url
= fetch_channel(v
, self
)
189 table
, new_gitpin
= parse_channel(v
, channel_html
)
190 if old_pin
is not None and old_pin
.git_revision
== new_gitpin
.git_revision
:
192 fetch_resources(v
, new_gitpin
, forwarded_url
, table
)
193 git_cache
.ensure_rev_available(
194 self
.git_repo
, self
.git_ref
, new_gitpin
.git_revision
)
195 if old_pin
is not None:
197 v
, self
, old_pin
.git_revision
, new_gitpin
.git_revision
)
198 check_channel_contents(v
, self
, table
, new_gitpin
)
200 release_name
=new_gitpin
.release_name
,
201 tarball_url
=table
['nixexprs.tar.xz'].absolute_url
,
202 tarball_sha256
=table
['nixexprs.tar.xz'].digest
,
203 git_revision
=new_gitpin
.git_revision
)
205 # pylint: disable=no-self-use
206 def fetch(self
, v
: Verification
, pin
: Pin
) -> str:
207 assert isinstance(pin
, ChannelPin
)
209 return fetch_with_nix_prefetch_url(
210 v
, pin
.tarball_url
, Digest16(pin
.tarball_sha256
))
213 SearchPath
= Union
[AliasSearchPath
,
217 TarrableSearchPath
= Union
[GitSearchPath
, ChannelSearchPath
]
220 def compare(a
: str, b
: str) -> Tuple
[List
[str], List
[str], List
[str]]:
222 def throw(error
: OSError) -> None:
225 def join(x
: str, y
: str) -> str:
226 return y
if x
== '.' else os
.path
.join(x
, y
)
228 def recursive_files(d
: str) -> Iterable
[str]:
229 all_files
: List
[str] = []
230 for path
, dirs
, files
in os
.walk(d
, onerror
=throw
):
231 rel
= os
.path
.relpath(path
, start
=d
)
232 all_files
.extend(join(rel
, f
) for f
in files
)
233 for dir_or_link
in dirs
:
234 if os
.path
.islink(join(path
, dir_or_link
)):
235 all_files
.append(join(rel
, dir_or_link
))
238 def exclude_dot_git(files
: Iterable
[str]) -> Iterable
[str]:
239 return (f
for f
in files
if not f
.startswith('.git/'))
241 files
= functools
.reduce(
244 recursive_files(x
))) for x
in [a
, b
]))
245 return filecmp
.cmpfiles(a
, b
, files
, shallow
=False)
249 v
: Verification
, channel
: ChannelSearchPath
) -> Tuple
[str, str]:
250 v
.status(f
'Fetching channel from {channel.channel_url}')
251 with urllib
.request
.urlopen(channel
.channel_url
, timeout
=10) as request
:
252 channel_html
= request
.read().decode()
253 forwarded_url
= request
.geturl()
254 v
.result(request
.status
== 200)
255 v
.check('Got forwarded', channel
.channel_url
!= forwarded_url
)
256 return channel_html
, forwarded_url
259 def parse_channel(v
: Verification
, channel_html
: str) \
260 -> Tuple
[Dict
[str, ChannelTableEntry
], GitPin
]:
261 v
.status('Parsing channel description as XML')
262 d
= xml
.dom
.minidom
.parseString(channel_html
)
265 v
.status('Extracting release name:')
266 title_name
= d
.getElementsByTagName(
267 'title')[0].firstChild
.nodeValue
.split()[2]
268 h1_name
= d
.getElementsByTagName('h1')[0].firstChild
.nodeValue
.split()[2]
270 v
.result(title_name
== h1_name
)
272 v
.status('Extracting git commit:')
273 git_commit_node
= d
.getElementsByTagName('tt')[0]
274 git_revision
= git_commit_node
.firstChild
.nodeValue
275 v
.status(git_revision
)
277 v
.status('Verifying git commit label')
278 v
.result(git_commit_node
.previousSibling
.nodeValue
== 'Git commit ')
280 v
.status('Parsing table')
281 table
: Dict
[str, ChannelTableEntry
] = {}
282 for row
in d
.getElementsByTagName('tr')[1:]:
283 name
= row
.childNodes
[0].firstChild
.firstChild
.nodeValue
284 url
= row
.childNodes
[0].firstChild
.getAttribute('href')
285 size
= int(row
.childNodes
[1].firstChild
.nodeValue
)
286 digest
= Digest16(row
.childNodes
[2].firstChild
.firstChild
.nodeValue
)
287 table
[name
] = ChannelTableEntry(url
=url
, digest
=digest
, size
=size
)
289 return table
, GitPin(release_name
=title_name
, git_revision
=git_revision
)
292 def digest_string(s
: bytes) -> Digest16
:
293 return Digest16(hashlib
.sha256(s
).hexdigest())
296 def digest_file(filename
: str) -> Digest16
:
297 hasher
= hashlib
.sha256()
298 with open(filename
, 'rb') as f
:
299 # pylint: disable=cell-var-from-loop
300 for block
in iter(lambda: f
.read(4096), b
''):
302 return Digest16(hasher
.hexdigest())
306 def _experimental_flag_needed(v
: Verification
) -> bool:
307 v
.status('Checking Nix version')
308 process
= subprocess
.run(['nix', '--help'], stdout
=subprocess
.PIPE
)
309 v
.result(process
.returncode
== 0)
310 return b
'--experimental-features' in process
.stdout
313 def _nix_command(v
: Verification
) -> List
[str]:
314 return ['nix', '--experimental-features',
315 'nix-command'] if _experimental_flag_needed(v
) else ['nix']
318 def to_Digest16(v
: Verification
, digest32
: Digest32
) -> Digest16
:
319 v
.status('Converting digest to base16')
320 process
= subprocess
.run(_nix_command(v
) + [
325 stdout
=subprocess
.PIPE
)
326 v
.result(process
.returncode
== 0)
327 return Digest16(process
.stdout
.decode().strip())
330 def to_Digest32(v
: Verification
, digest16
: Digest16
) -> Digest32
:
331 v
.status('Converting digest to base32')
332 process
= subprocess
.run(_nix_command(v
) + [
337 stdout
=subprocess
.PIPE
)
338 v
.result(process
.returncode
== 0)
339 return Digest32(process
.stdout
.decode().strip())
342 def fetch_with_nix_prefetch_url(
345 digest
: Digest16
) -> str:
346 v
.status(f
'Fetching {url}')
347 process
= subprocess
.run(
348 ['nix-prefetch-url', '--print-path', url
, digest
], stdout
=subprocess
.PIPE
)
349 v
.result(process
.returncode
== 0)
350 prefetch_digest
, path
, empty
= process
.stdout
.decode().split('\n')
352 v
.check("Verifying nix-prefetch-url's digest",
353 to_Digest16(v
, Digest32(prefetch_digest
)) == digest
)
354 v
.status(f
"Verifying digest of {path}")
355 file_digest
= digest_file(path
)
356 v
.result(file_digest
== digest
)
357 return path
# type: ignore # (for old mypy)
364 table
: Dict
[str, ChannelTableEntry
]) -> None:
365 for resource
in ['git-revision', 'nixexprs.tar.xz']:
366 fields
= table
[resource
]
367 fields
.absolute_url
= urllib
.parse
.urljoin(forwarded_url
, fields
.url
)
368 fields
.file = fetch_with_nix_prefetch_url(
369 v
, fields
.absolute_url
, fields
.digest
)
370 v
.status('Verifying git commit on main page matches git commit in table')
371 with open(table
['git-revision'].file, encoding
='utf-8') as rev_file
:
372 v
.result(rev_file
.read(999) == pin
.git_revision
)
375 def tarball_cache_file(channel
: TarrableSearchPath
, pin
: GitPin
) -> str:
379 f
'{digest_string(channel.git_repo.encode())}-{pin.git_revision}-{pin.release_name}')
382 def verify_git_ancestry(
384 channel
: TarrableSearchPath
,
386 new_revision
: str) -> None:
387 cachedir
= git_cache
.git_cachedir(channel
.git_repo
)
388 v
.status(f
'Verifying rev is an ancestor of previous rev {old_revision}')
389 process
= subprocess
.run(['git',
396 v
.result(process
.returncode
== 0)
399 def compare_tarball_and_git(
402 channel_contents
: str,
403 git_contents
: str) -> None:
404 v
.status('Comparing channel tarball with git checkout')
405 match
, mismatch
, errors
= compare(os
.path
.join(
406 channel_contents
, pin
.release_name
), git_contents
)
408 v
.check(f
'{len(match)} files match', len(match
) > 0)
409 v
.check(f
'{len(mismatch)} files differ', len(mismatch
) == 0)
417 for ee
in expected_errors
:
420 benign_errors
.append(ee
)
421 v
.check(f
'{len(errors)} unexpected incomparable files', len(errors
) == 0)
423 f
'({len(benign_errors)} of {len(expected_errors)} expected incomparable files)',
424 len(benign_errors
) == len(expected_errors
))
429 table
: Dict
[str, ChannelTableEntry
],
431 v
.status(f
"Extracting tarball {table['nixexprs.tar.xz'].file}")
432 shutil
.unpack_archive(table
['nixexprs.tar.xz'].file, dest
)
438 channel
: TarrableSearchPath
,
441 v
.status('Checking out corresponding git revision')
442 with subprocess
.Popen(
443 ['git', '-C', git_cache
.git_cachedir(channel
.git_repo
), 'archive', pin
.git_revision
],
444 stdout
=subprocess
.PIPE
) as git
:
445 with subprocess
.Popen(['tar', 'x', '-C', dest
, '-f', '-'], stdin
=git
.stdout
) as tar
:
450 v
.result(git
.returncode
== 0 and tar
.returncode
== 0)
455 channel
: TarrableSearchPath
,
457 cache_file
= tarball_cache_file(channel
, pin
)
458 if os
.path
.exists(cache_file
):
459 with open(cache_file
, encoding
='utf-8') as f
:
460 cached_tarball
= f
.read(9999)
461 if os
.path
.exists(cached_tarball
):
462 return cached_tarball
464 with tempfile
.TemporaryDirectory() as output_dir
:
465 output_filename
= os
.path
.join(
466 output_dir
, pin
.release_name
+ '.tar.xz')
467 with open(output_filename
, 'w', encoding
='utf-8') as output_file
:
468 v
.status(f
'Generating tarball for git revision {pin.git_revision}')
469 with subprocess
.Popen(
470 ['git', '-C', git_cache
.git_cachedir(channel
.git_repo
),
471 'archive', f
'--prefix={pin.release_name}/', pin
.git_revision
],
472 stdout
=subprocess
.PIPE
) as git
:
473 with subprocess
.Popen(['xz'], stdin
=git
.stdout
, stdout
=output_file
) as xz
:
476 v
.result(git
.returncode
== 0 and xz
.returncode
== 0)
478 store_tarball
= copy_to_nix_store(v
, output_filename
)
480 os
.makedirs(os
.path
.dirname(cache_file
), exist_ok
=True)
481 with open(cache_file
, 'w', encoding
='utf-8') as f
:
482 f
.write(store_tarball
)
483 return store_tarball
# type: ignore # (for old mypy)
486 def check_channel_metadata(
489 channel_contents
: str) -> None:
490 v
.status('Verifying git commit in channel tarball')
491 with open(os
.path
.join(channel_contents
, pin
.release_name
, '.git-revision'),
492 encoding
='utf-8') as f
:
493 v
.result(f
.read(999) == pin
.git_revision
)
496 f
'Verifying version-suffix is a suffix of release name {pin.release_name}:')
497 with open(os
.path
.join(channel_contents
, pin
.release_name
, '.version-suffix'),
498 encoding
='utf-8') as f
:
499 version_suffix
= f
.read(999)
500 v
.status(version_suffix
)
501 v
.result(pin
.release_name
.endswith(version_suffix
))
504 def check_channel_contents(
506 channel
: TarrableSearchPath
,
507 table
: Dict
[str, ChannelTableEntry
],
508 pin
: GitPin
) -> None:
509 with tempfile
.TemporaryDirectory() as channel_contents
, \
510 tempfile
.TemporaryDirectory() as git_contents
:
512 extract_tarball(v
, table
, channel_contents
)
513 check_channel_metadata(v
, pin
, channel_contents
)
515 git_checkout(v
, channel
, pin
, git_contents
)
517 compare_tarball_and_git(v
, pin
, channel_contents
, git_contents
)
519 v
.status('Removing temporary directories')
523 def git_revision_name(
525 channel
: TarrableSearchPath
,
526 git_revision
: str) -> str:
527 v
.status('Getting commit date')
528 process
= subprocess
.run(['git',
530 git_cache
.git_cachedir(channel
.git_repo
),
535 '--no-show-signature',
537 stdout
=subprocess
.PIPE
)
538 v
.result(process
.returncode
== 0 and process
.stdout
!= b
'')
539 return f
'{os.path.basename(channel.git_repo)}-{process.stdout.decode().strip()}'
546 def partition_dict(pred
: Callable
[[K
, V
], bool],
547 d
: Dict
[K
, V
]) -> Tuple
[Dict
[K
, V
], Dict
[K
, V
]]:
548 selected
: Dict
[K
, V
] = {}
549 remaining
: Dict
[K
, V
] = {}
550 for k
, v
in d
.items():
555 return selected
, remaining
558 def filter_dict(d
: Dict
[K
, V
], fields
: Set
[K
]
559 ) -> Tuple
[Dict
[K
, V
], Dict
[K
, V
]]:
560 return partition_dict(lambda k
, v
: k
in fields
, d
)
563 def read_config_section(
564 conf
: configparser
.SectionProxy
) -> Tuple
[SearchPath
, Optional
[Pin
]]:
565 mapping
: Mapping
[str, Tuple
[Type
[SearchPath
], Type
[Pin
]]] = {
566 'alias': (AliasSearchPath
, AliasPin
),
567 'channel': (ChannelSearchPath
, ChannelPin
),
568 'git': (GitSearchPath
, GitPin
),
569 'symlink': (SymlinkSearchPath
, SymlinkPin
),
571 SP
, P
= mapping
[conf
['type']]
572 _
, all_fields
= filter_dict(dict(conf
.items()), set(['type']))
573 pin_fields
, remaining_fields
= filter_dict(all_fields
, set(P
._fields
))
574 # Error suppression works around https://github.com/python/mypy/issues/9007
575 pin_present
= pin_fields
or P
._fields
== ()
576 pin
= P(**pin_fields
) if pin_present
else None # type: ignore
577 return SP(**remaining_fields
), pin
580 def read_pinned_config_section(
581 section
: str, conf
: configparser
.SectionProxy
) -> Tuple
[SearchPath
, Pin
]:
582 sp
, pin
= read_config_section(conf
)
585 f
'Cannot update unpinned channel "{section}" (Run "pin" before "update")')
589 def read_config(filename
: str) -> configparser
.ConfigParser
:
590 config
= configparser
.ConfigParser()
591 with open(filename
, encoding
='utf-8') as f
:
592 config
.read_file(f
, filename
)
596 def read_config_files(
597 filenames
: Iterable
[str]) -> Dict
[str, configparser
.SectionProxy
]:
598 merged_config
: Dict
[str, configparser
.SectionProxy
] = {}
599 for file in filenames
:
600 config
= read_config(file)
601 for section
in config
.sections():
602 if section
in merged_config
:
603 raise Exception('Duplicate channel "{section}"')
604 merged_config
[section
] = config
[section
]
608 def pinCommand(args
: argparse
.Namespace
) -> None:
610 config
= read_config(args
.channels_file
)
611 for section
in config
.sections():
612 if args
.channels
and section
not in args
.channels
:
615 sp
, old_pin
= read_config_section(config
[section
])
617 config
[section
].update(sp
.pin(v
, old_pin
)._asdict
())
619 with open(args
.channels_file
, 'w', encoding
='utf-8') as configfile
:
620 config
.write(configfile
)
623 def updateCommand(args
: argparse
.Namespace
) -> None:
625 exprs
: Dict
[str, str] = {}
626 profile_manifest
= os
.path
.join(args
.profile
, "manifest.nix")
627 search_paths
: List
[str] = [
628 "-I", "pinch_profile=" + args
.profile
,
629 "-I", "pinch_profile_manifest=" + os
.readlink(profile_manifest
)
630 ] if os
.path
.exists(profile_manifest
) else []
632 section
: read_pinned_config_section(section
, conf
) for section
,
633 conf
in read_config_files(
634 args
.channels_file
).items()}
635 alias
, nonalias
= partition_dict(
636 lambda k
, v
: isinstance(v
[0], AliasSearchPath
), config
)
638 for section
, (sp
, pin
) in sorted(nonalias
.items()):
639 assert not isinstance(sp
, AliasSearchPath
) # mypy can't see through
640 assert not isinstance(pin
, AliasPin
) # partition_dict()
641 tarball
= sp
.fetch(v
, pin
)
643 ["-I", f
"pinch_tarball_for_{pin.release_name}={tarball}"])
645 f
'f: f {{ name = "{pin.release_name}"; channelName = "%s"; '
646 f
'src = builtins.storePath "{tarball}"; }}')
648 for section
, (sp
, pin
) in alias
.items():
649 assert isinstance(sp
, AliasSearchPath
) # For mypy
650 exprs
[section
] = exprs
[sp
.alias_of
]
658 '<nix/unpack-channel.nix>',
661 ] + search_paths
+ ['--from-expression'] + [
662 exprs
[name
] % name
for name
in sorted(exprs
.keys())]
664 print(' '.join(map(shlex
.quote
, command
)))
666 v
.status('Installing channels with nix-env')
667 process
= subprocess
.run(command
)
668 v
.result(process
.returncode
== 0)
672 parser
= argparse
.ArgumentParser(prog
='pinch')
673 subparsers
= parser
.add_subparsers(dest
='mode', required
=True)
674 parser_pin
= subparsers
.add_parser('pin')
675 parser_pin
.add_argument('channels_file', type=str)
676 parser_pin
.add_argument('channels', type=str, nargs
='*')
677 parser_pin
.set_defaults(func
=pinCommand
)
678 parser_update
= subparsers
.add_parser('update')
679 parser_update
.add_argument('--dry-run', action
='store_true')
680 parser_update
.add_argument('--profile', default
=(
681 f
'/nix/var/nix/profiles/per-user/{getpass.getuser()}/channels'))
682 parser_update
.add_argument('channels_file', type=str, nargs
='+')
683 parser_update
.set_defaults(func
=updateCommand
)
684 args
= parser
.parse_args()
688 if __name__
== '__main__':